【问题标题】:webshot/phantom pdf, printing only second half of the pagewebshot/phantom pdf,仅打印页面的后半部分
【发布时间】:2015-08-19 22:27:53
【问题描述】:

我正在使用 webshot npm 模块来创建我的页面的 pdf 文件

这是我的页面

我将其作为 pdf 格式输出

我的设置是

    var options = {
                  renderDelay:10000,
                    "paperSize": {
                        "format": "Letter", 
                        "orientation": "portrait", 
                        "border": "1cm"
                    },
                    shotSize: {
                        width: 'all',
                        height: 'all'
                    },
                    shotOffset: {
                        left: 0
                        , right: 0
                        , top: 0
                        , bottom: 0
                    }
                };

                webshot(url, fileName, options, function(err) {
                  fs.readFile(fileName, function (err,data) {
                    if (err) {
                      return console.log(err);
                    }

                    fs.unlinkSync(fileName);
                    fut.return(data);
                  });
                });

            this.response.writeHead(200, {'Content-Type': 'application/pdf',"Content-Disposition": "attachment; filename=generated.pdf"});
this.response.end(fut.wait());

对于流星人,这是我的服务器端根

this.route('generatePDF', {
        path: '/api/generatePDF',
        where: 'server',
        action: function() {
            var webshot = Meteor.npmRequire('webshot');
            var fs      = Npm.require('fs');
            Future = Npm.require('fibers/future');
            var fut = new Future();
            var fileName = "generated_"+Random.id()+".pdf";
            var userid = (Meteor.isClient) ? Meteor.userId() : this.userId;
            console.log(userid);
            // var username = Meteor.users.findOne({_id: userid}).username;
            var url = "url";

            var options = {
              renderDelay:10000,
                "paperSize": {
                    "format": "Letter", 
                    "orientation": "portrait", 
                    "border": "1cm"
                },
                shotSize: {
                    width: 'all',
                    height: 'all'
                },
                shotOffset: {
                    left: 0
                    , right: 0
                    , top: 0
                    , bottom: 0
                }
            };

            webshot(url, fileName, options, function(err) {
              fs.readFile(fileName, function (err,data) {
                if (err) {
                  return console.log(err);
                }

                fs.unlinkSync(fileName);
                fut.return(data);
              });
            });

            this.response.writeHead(200, {'Content-Type': 'application/pdf',"Content-Disposition": "attachment; filename=generated.pdf"});
            this.response.end(fut.wait());
        }
    });

我在这里遗漏了什么吗?任何帮助表示赞赏

【问题讨论】:

  • 我看不出你的代码有什么问题。也许网站、PhantomJS 或两者都坏了。也许通过改变 shotOffset 有一种解决方法

标签: node.js pdf meteor phantomjs


【解决方案1】:

您可以尝试将 paperSize 更改为:

"paperSize": {
                width: '612px',
                height: '792px',
                margin: 'XXpx'
            },

【讨论】:

    【解决方案2】:

    如果其他人对此有问题 - 我也有同样的问题。我无法确切地告诉你原因,但问题是我使用的是引导程序并且我的页面的包装器有“容器”类。移除这个类后,整个页面都被渲染了——没有移除它,它只是渲染了页面的一半。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2014-06-08
      相关资源
      最近更新 更多