zoukankan      html  css  js  c++  java
  • Phantomjs 生成多页PDF

    开篇

    最近使用 Phantomjs 生成PDF,其中遇到一些问题,导致PDF生成失败,如出现空白文件或一页数据量太大,都是由于没有设置好格式导致。特别是分页问题,感觉资料很少,除了在 StackOverflow 上看到些许资料外,中文社区基本看不到,附上修改后的 rasterize.js 来做讲解:

     1 var page = require('webpage').create(),
     2     system = require('system'),
     3     address, output, size;
     4 
     5 if (system.args.length < 3 || system.args.length > 5) {
     6     console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
     7     console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
     8     phantom.exit(1);
     9 } else {
    10     address = system.args[1];
    11     output = system.args[2];
    12     /*size of browser*/
    13     page.viewportSize = {  600, height: 600 };
    14     /*
    15     if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
    16         size = system.args[3].split('*');
    17         page.paperSize = size.length === 2 ? {  size[0], height: size[1], margin: '0px' }
    18                                            : { format: 'A4', orientation: 'portrait', margin: '1cm' };
    19     }
    20     */
    21     /* ie and chrome view diffrent format of pdf */
    22     page.settings.userAgent = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36';
    23     page.paperSize = { format: 'A4', orientation: 'portrait', margin: '0.8cm' };
    24     page.zoomFactor = 1;
    25     page.settings.loadImages = true;
    26     //some question about the page language
    27     page.open(address, function (status) {
    28         if (status !== 'success') {
    29             console.log('Unable to load the address!');
    30         } else {
    31             //page.render(output);
    32             //phantom.exit();
    33             
    34             window.setTimeout(function () {
    35                 page.render(output);
    36                 phantom.exit();
    37             }, 200); //setting the time is enough to loading the page. document.ready
    38             
    39         }
    40     });
    41 }
    View Code

    PDF 格式设置

    关于其中 page 的设置属性,这里可以了解,更深入可以了解 WebPage Module

    我们需要的设置,基本上就是页面格式、缩放、加载图片等,但有些例外,下面一一讲解。

    1 page.paperSize = { format: 'A4', orientation: 'portrait', margin: '0.8cm' };

    注释掉了官方例子的设置代码,因为传入的参数只有3个,到 .pdf 为止,如果写成通用模式,当然可以作为外部参数传入。

    format :A4 纸,可以设置 "5in*7.5in", "10cm*20cm",  "Letter" 等

    orientation :纸方向是竖着的,或者 landscape

    margin :与纸四边间距,可自定义,也可详细设置 margin : { left: '0.8cm',  top : '0.8cm',  right : '0.8cm',  bottom : '0.8cm' }

    1 page.zoomFactor = 1;
    2 page.settings.loadImages = true;

    zoomFactor :页面缩放比例

    loadImages :页面加载图片

    1 page.settings.userAgent = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36';

    这个设置比较不常见,一般的示例中都没有提及,因为发现用 chrome 和 IE 打开生成的 pdf 时格式有点不一样(表现在分页方面),由于偏向 Chrome 浏览格式,故设置此值,解决这个不一致问题。 

    page.open 里面的 setTimeout 方法作用:等待页面执行完 js ,再生成 pdf。当然对于 js 要执行多久(要等多久),这个就不知道怎么预算了。其实我有试过 ajax 方式加载内容,但因此问题而作罢了。

    更多的信息,关于页眉和页脚及页码标注问题,可以参考这里

    PDF 分页

    分页来说,更好控制,不需要代码(js)设置,页面使用样式即可:

    style = “page-break-after: always;”
    

    控制每页内容的大小,使用 <div style="page-break-after: always;">content</div> 就行。

    更多选择 style=“page-break-before: always;” , style="page-break-inside: avoid;" 这个可以避免内容散到两页中

    总结

    关于这个 phantomjs pdf render 就到此了,如有更多好的方式及问题解决方案,欢迎大家分享。

  • 相关阅读:
    冒泡法排序(整数)
    system函数的应用
    数数的位数(正整数)
    翻译Sencha Touch Architecture 第二章
    BeeFramework
    Creating a masterdetail application with tableview
    翻译Sencha Touch Mobile Javascript Framework 第八章 The Flickr Finder Application
    SDWebImage笔记
    Ubuntu aptget方式安装Subversion
    Python统计svn变更代码行数
  • 原文地址:https://www.cnblogs.com/ranmofang/p/3752857.html
Copyright © 2011-2022 走看看