【问题标题】:PhantomJS converting HTML to JPEG: Image is cut offPhantomJS 将 HTML 转换为 JPEG:图像被截断
【发布时间】:2017-01-18 20:19:22
【问题描述】:

我正在尝试使用 phantomjs 将 HTML 页面转换为 JPEG,并且底部的 输出图像被截断(图像不显示完整的 HTML 页面) .

PhantomJS

这是我的 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);
        }
    });

我运行它如下:

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

HTML

我要导出的 HTML 页面使用 jointjs 将 SVG 绘制到页面上,因此它包含一个 <svg> 标记以及 <g> 标记,然后添加一些普通的 <div><table>等标签。

以下是 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>

在浏览器中查看输入的 HTML 页面时,整个页面都在视图中/HTML 页面中没有任何内容被截断

图片

生成的图像显示了 HTML 页面中的 &lt;table&gt;,但它被截断了!应该显示整个表格。它应该上升到“e”,而不是切断“d”行之一。在实际的 HTML 页面中(在浏览器中查看),表格正确显示并上升到“e”:

有人知道为什么我的图片被截断了吗?

【问题讨论】:

  • 您能否在某处提供整页示例进行实验?另外,PhantomJS 是什么版本?

标签: javascript html svg phantomjs jointjs


【解决方案1】:

嗯,原来是jointJS问题,根本不是PhantomJS问题!对于那些可能发现这个问题的人,我仍然会发布解决方案。

当一个元素被用户移动或创建时,它可能会在画布之外创建。所以我听“鼠标指针向上”事件并检查它是否不在画布上。如果它关闭,那么我展开画布。 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));
     }
});

【讨论】:

    猜你喜欢
    • 2022-01-27
    • 2013-04-15
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多