简体   繁体   English

飞碟:将多个 Html 转换为 1 个 PDF 文档

[英]Flying Saucer: Convert Multiple Html to 1 PDF document

I have an Report template which is comprised with 4 HTML file.我有一个包含 4 个 HTML 文件的报告模板。 Let say让我们说

  1. p1.html p1.html
  2. p2.html p2.html
  3. p3.html p3.html
  4. p4.html p4.html

I parse each page separately using Flying Saucer and replace the place holder using velocity and convert it to pdf sucessfully.我使用飞碟分别解析每个页面并使用速度替换占位符并将其成功转换为 pdf。 Now the problem is to convert these 4 HTML pages to a single pdf document.现在的问题是将这 4 个 HTML 页面转换为单个 pdf 文档。

There are 2 ways to do achieve that.有两种方法可以做到这一点。

Merging HTML合并 HTML

By merging all HTML document and then populating the placeholder using velocity which is being done successfully but problem arises in pagination.通过合并所有 HTML 文档,然后使用速度填充占位符,这已成功完成,但分页出现问题。 I want each HTML page converted to 1 pdf page but in this scenario all text are merged.我希望将每个 HTML 页面转换为 1 个 pdf 页面,但在这种情况下,所有文本都被合并。

Merge PDF after Conversion转换后合并 PDF

This approach doesn't seem right to me because generating each PDF page separately from respective HTML page and then merging to 1 pdf document is not an enhance-able solution.这种方法对我来说似乎不合适,因为从相应的 HTML 页面分别生成每个 PDF 页面然后合并到 1 个 pdf 文档不是一个可增强的解决方案。

Your suggestions with coding examples will be highly appreciated.您对编码示例的建议将不胜感激。

I'm looking for a way to combine documents too.我也在寻找一种合并文档的方法。

I do know how to start new pages while using a single HTML document though.不过,我确实知道如何在使用单个 HTML 文档时开始新页面。 Try this:尝试这个:

<html>
<head>
    <style>
        @page { /* default page styles here */

        @page p1 { /* page template for first document */ }
        @page p2 { /* page template for second document */ }
        @page p3 { /* page template for third document */ }
        @page p4 { /* page template for fourth document */ }

        #p1 { page: p1; } /* tells #p1 to use p1 page template */
        #p2 { page: p2; } /* tells #p2 to use p2 page template */
        #p3 { page: p3; } /* tells #p3 to use p3 page template */
        #p4 { page: p4; } /* tells #p4 to use p4 page template */
    </style>
</head>

<body>
</body>

    <article id="p1">
        <!-- page 1 content here -->
    </article>

    <article id="p2">
        <!-- page 2 content here -->
    </article>

    <article id="p3">
        <!-- page 3 content here -->
    </article>

    <article id="p4">
        <!-- page 4 content here -->
    </article>

</html>

My understanding is that when an element requires a different page template, then a page break will get added in.我的理解是,当一个元素需要不同的页面模板时,就会添加分页符。

If you don't need different page templates (the headers, footers, etc. are all the same) then you can probably just ask for a page break for each article:如果您不需要不同的页面模板(页眉、页脚等都相同),那么您可能只需为每篇文章请求分页符:

article { page-break-before: always; }

I opted for merging HTML templates and used following CSS to paginate it where it is required.我选择合并 HTML 模板并使用以下 CSS 将其分页到需要的地方。

    <style type="text/css"> 
    @page { size:letter; padding:0; margin:0.5in 25px 100px 25px;}
    *{ font-family: "verdana", tahoma, arial, sans-serif;}
     table { -fs-table-paginate: paginate; thead {
    display:table-header-group;}}
    @page {
         @top-center { content: element(header) }
    }
    @page:first {
        margin:30px 25px 100px 25px;
         @top-center { content: element() }
    }
    table.header {
        height:100px;
        display: block; text-align: center; 
        position: running(header);
    }
    div.footer {
        display: block; text-align: center;
        position: running(footer);
    }
    div.content {page-break-after: always;}

     #footer {
    position: running(footer);
    text-align: right;
    }

    #pagenumber:before {
    content: counter(page);  }

    #pagecount:before {
    content: counter(pages);  }


</style>

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

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