javaitextflying-saucer

Flying Saucer: Convert Multiple Html to 1 PDF document


I have an Report template which is comprised with 4 HTML file. Let say

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

I parse each page separately using Flying Saucer and replace the place holder using velocity and convert it to pdf sucessfully. Now the problem is to convert these 4 HTML pages to a single pdf document.

There are 2 ways to do achieve that.

Merging HTML

By merging all HTML document and then populating the placeholder using velocity which is being done successfully but problem arises in pagination. I want each HTML page converted to 1 pdf page but in this scenario all text are merged.

Merge PDF after Conversion

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.

Your suggestions with coding examples will be highly appreciated.


Solution

  • I opted for merging HTML templates and used following CSS to paginate it where it is required.

        <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>