jsreport / jsreport-pdf-utils

jsreport extension providing pdf operations like merge or concatenation
MIT License
8 stars 4 forks source link

Merge header into one landscape and one portrait page #11

Open pofider opened 6 years ago

pofider commented 6 years ago

Original problem described here https://forum.jsreport.net/topic/669/header-did-not-be-applied-to-the-landscape-pages-appended-by-pdfoperation

The problem is that we need for merge both pdf pages to have same size. I am not sure if we can overcome this limitation. However perhaps there is another way how to support merging headers into mixed page orientation pdfs.

pofider commented 6 years ago

Unfortunately this seems to be quite problematic technically. Even if I can manage to adapt the page size before merging, the inner content has still the original size and I don't think it makes sense to hack it now.

I mention some modifications I have done during the investigation, perhaps it will be helpful once.

  1. Take the mediabox size for the merged xobj from the page we are merging into https://github.com/jsreport/jsreport-pdf-utils/blob/1.1.0-beta/lib/utils/EmbeddedPage.js#L11
module.exports = class EmbeddedPdf {
  constructor (page, mergeDestPage) {   
    const mediaBox = mergeDestPage.object.properties.get('MediaBox')
  1. Fix the cm... instruction to adapt the size in the merged stream https://github.com/jsreport/jsreport-pdf-utils/blob/1.1.0-beta/lib/utils/mergePdfs.js#L48
    mergeStream.content = mergeStream.content.replace('792', '612')
shellord commented 8 months ago

Is there any solution or workaround for this ? I need to merge header and footer in a pdf which have both portrait and landscape pages.

manemal commented 7 months ago

I created a sample here: https://playground.jsreport.net/w/anon/DThi3uoK

This essentially uses {{{pdfAddPageItem name="Portrait" value=true/false }}} in the content templates to indicate orientation Then two merge passes for the header/footers, one for landscape pages, other for portrait page - using helper function {{#if (isPortrait this)}} to determine whether to apply header/footer template to the current page.

It works but isn't always perfect. Sometimes content pushed into another page doesn't get the pdfAddPageItem key/value applied.

There was also this example from Jan: https://playground.jsreport.net/w/anon/tApz4jtt Here Jan is using the

<script>  
    window.JSREPORT_CHROME_PDF_OPTIONS = {
        landscape:  {{landscape}}
    }
</script>

In the header/footer template to determine orientation dynamically. Perhaps this could be used when enumerating through the $pdf.pages array. Haven't had time to investigate further...

For reference, I found Jan's demo in this forum post from 3 years ago: https://forum.jsreport.net/topic/2014/sync-orientation-of-header-with-document-using-pdfutils/3