Adjust page size for result PDF to size of placed PDF
Sometimes the dimensions of the result PDF have to be adjusted to an object. This, for instance, is required when the purpose is to create variants from an input PDF where each of the variants has some additional content. In this article we describe how to create variants from an incoming PDF where each variant has an individual QR code. The trick is how to adjust the size of the result PDF variants to the size of the incoming PDF.
The example archive has just 4 files:
- input.pdf is the placed PDF and can be replaced with any other PDF
- index.html is a very small HTML template
- params.js defines some static parameters for the QR code and the total number of pages
- qrcode.js has the logic that adjusts the page size, places the QR codes and creates pages
The HTML template is very simple.
- The PDF file is placed
- A DIV container is created for the QR code
- jobID, customerID and addvalues are static values that are the same for all pages of the result PDF
- numberofpages defines the required number of different page variants form the input PDF
The first function "adjustDocumentSizeToHtmlElement" adjusts the size of the HTML template to the size of the placed PDF
- The placed PDF was contained in an img tag with the id "inputPDF". This will be used when adjustDocumentSizeToHtmlElement is called as the "inElementID".
The dimensions of this object are determined and written into two varialbes.
- A margin is defined to use --cchip-cropbox to make the result page 500pt bigger than the required size and to then crop it to that required size.
- A style element for the page size @page is created and appended and the required size is written into this @page element. In addition the --cchip-cropbox is defined.
The second function is the cchipPrintLoop function that allows for creating pages. In this function:
- The first function adjustDocumentSizeToHtmlElement is called with "inputPDF" as parameter (that defines the object from which the page size has to be derived as described above).
- A loop is started for the number of required page variants
- The QR code value is concatenated from the static values in params.js and the counter that is individual for each page variant. In this example the counter is just made part of the QR code value.
- The QR code object is created and written into the HTML object with id "QR_Code".
- A page is created via cchip.printPages.