简体   繁体   English

PhantomJS将HTML转换为JPEG:图像被截断

[英]PhantomJS converting HTML to JPEG: Image is cut off

I am trying to convert an HTML page to a JPEG using phantomjs and the output image is cut off at the bottom (image doesn't display full HTML page). 我正在尝试使用phantomjs 将HTML页面转换为JPEG ,并且输出图像在底部被截断 (图像不显示完整的HTML页面)。

PhantomJS PhantomJS

Here's my code for rasterize.js 这是我的rasterize.js代码

var page = require('webpage').create(),
    system = require('system'),
    address, output, size;

    address = system.args[1];
    output = system.args[2];

    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit(1);
        } else {
            window.setTimeout(function () {
                page.render(output);
                phantom.exit();
            }, 200);
        }
    });

and I run it as follows: 我运行如下:

phantomjs ./rasterize.js ./test.html ./test.jpg

HTML HTML

the HTML page I am trying to export uses jointjs which draws SVGs to the page so it contains a <svg> tag, as well as <g> tags, then add in some normal <div> , <table> , etc. tags. 我试图导出的HTML页面使用jointjs ,它将<svg>绘制到页面,因此它包含<svg>标签,以及<g>标签,然后添加一些普通的<div><table>等标签。

Here's some examples from the HTML page: 以下是HTML页面中的一些示例:

...
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="v-2" width="3100" height="1101" visibility="shown">
<defs id="v-4">
<linearGradient id="linearGradientv-2-107112646">
<stop offset="0%" stop-color="rgb(228,234,243)" stop-opacity="1"/>
<stop offset="60%" stop-color="rgb(201,212,231)" stop-opacity="1"/>
</linearGradient>
...
<g id="j_1" model-id="86b320b6-0e8a-4dee-8258-e329b97c04ea" class="element custom Device" magnet="false" fill="#FFFFFF" stroke="none" transform="translate(50,100)">
<g class="rotatable" id="v-6" transform="rotate(0,150,20)">
<g class="scalable" id="v-47" transform="scale(2,0.16)">
<rect class="body" id="v-13" fill="url(#linearGradientv-2-107112646)" width="150" height="250" stroke="black"/>
</g>
...
</svg>

The input HTML page, when viewed in a browser, has the entire page in view / nothing is cut off in the HTML page . 在浏览器中查看时,输入HTML页面具有整个页面视图/ HTML页面中没有任何内容被截断

Image 图片

The resulting image shows the <table> from the HTML page, but it's cut off! 生成的图像显示HTML页面中的<table> ,但它已被切断! The entire table should show. 整个表格应该显示出来。 It should go up to "e", instead it cuts off one of the "d" rows. 它应该上升到“e”,而是切断其中一个“d”行。 In the actual HTML page (viewed in browser), the table is shown correctly and goes up to "e": 在实际的HTML页面中(在浏览器中查看),表格显示正确并上升到“e”:

图像被切断

Does anyone know why my image is cut off? 有谁知道为什么我的图像会被切断?

Well, it turned out to be a jointJS problem and not a PhantomJS problem at all! 好吧,它原来是一个联合问题而不是PhantomJS问题! For those that may find this, I'll still post the solution. 对于那些可能会发现这一点的人,我仍然会发布解决方案。

When an element is moved or created by the user, it can possibly be created off the canvas. 当用户移动或创建元素时,可以在画布上创建它。 So I listen to the "mouse pointer up" event and check if it's off the canvas. 所以我听“鼠标指针向上”事件并检查它是否离开画布。 If it is off, then I expand the canvas. 如果它关闭,那么我展开画布。 The JointJS code now looks like this: JointJS代码现在看起来像这样:

paper.on('cell:pointerup', function(cellView, evt, x, y) {
     if(x>(paper.options.width-150))
     {
         paper.setDimensions((paper.options.width+200),paper.options.height);
     }else if(y>paper.options.height)
     {
         paper.setDimensions(paper.options.width,(paper.options.height+200));
     }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM