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

index.html

The HTML template is very simple.

  1. Two JavaScript files are referenced
  2. The PDF file is placed
  3. A DIV container is created for the QR code

params.js

The params.js JavaScript just has a few parameters for the QR code. It can be overwritten for every job if needed and that is the reason why it is separate from the other JavaScript.

  1. jobID, customerID and addvalues are static values that are the same for all pages of the result PDF
  2. numberofpages defines the required number of different page variants form the input PDF

qrcode.js

This JavaScript file has all logic of this example. It consists of two functions.

The first function "adjustDocumentSizeToHtmlElement" adjusts the size of the HTML template to the size of the placed PDF

  1. 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.
  2. 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.
  3. 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:

  1. 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).
  2. A loop is started for the number of required page variants
  3. 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.
  4. The QR code object is created and written into the HTML object with id "QR_Code".
  5. A page is created via cchip.printPages.