Dynamic PDF Backgound Image with Different SIzes

I have a template in which I need to dynamically insert a PDF from a folder as the page background. The name of the PDF comes from a data field. But the PDF can be either 8.5 x 11 (portrait) or 17 x 11 (landscape) format and I don’t know in advance which one it will be (and it may vary record by record). I will be adding some content which depends on the PDF page size (some fields and position for one size and other fields/position for the alternative)

What is the best way to detect the page size of the PDF in order to activate the correct print section if I set up one section for each size? Or is there a better way to do it than to have separate print sections?

One way to achieve it would be to insert the PDF width and\or height into the data file as separates data fields using the Run Script action

Imagine the following pipe delimited CSV data file where the last field on each line is the name of the PDF

Mr AB Sample1|1 Sample Lane|Sample|County Sample|AA1 1AA|UK|11x17Landscape.pdf
Mr AB Sample2|2 Sample Lane|Sample|County Sample|AA2 2AA|UK|8.5x11Portrait.pdf
Mr AB Sample3|3 Sample Lane|Sample|County Sample|AA3 3AA|UK|11x17Landscape.pdf
Mr AB Sample4|4 Sample Lane|Sample|County Sample|AA4 4AA|UK|11x17Landscape.pdf
Mr AB Sample5|5 Sample Lane|Sample|County Sample|AA5 5AA|UK|8.5x11Portrait.pdf

The below VBScript loops through each record of the CSV, gets the the PDF name, opens it and read its width and height property and finally add these two values to the end of each record.

dim objFSO, objInputFile, objOutputFile
dim myCSV, strNewLineContent, strLine, fieldsArr
dim pdfBackground, myRect, pdfWidth, pdfHeight

set pdfBackground = Watch.GetPDFEditObject
set myRect = CreateObject(“AlambicEdit.PdfRect”)

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

myCSV = Watch.GetJobFileName
set objInputFile = objFSO.OpenTextFile(myCSV, 1, true, 0)
strNewLineContent = “”

    Do Until objInputFile.AtEndOfStream

            strLine = Trim(objInputFile.Readline)
            fieldsArr = Split(strLine, "|")
            pdfBackground.open "E:\Tickets\157678\" & fieldsArr(UBound(fieldsArr)),false
            Set myRect = pdfBackground.Pages(0).size
            pdfWidth   = Round((myRect.right)*0.0138889,2) 'points to inch conversion
            pdfHeight  = Round((myRect.top)*0.0138889,2) 'points to inch conversion
            strLine = strLine  &  "|" & pdfWidth &  "|"  & pdfHeight & vbCrLf
            strNewLineContent = strNewLineContent & strLine
            Watch.log " pdfWidth is: " & pdfWidth & " inch" & " and pdfHeight is: " & pdfHeight & " inch.", 2
            pdfBackground.close

    Loop

objInputFile.Close
set objOutputFile = objFSO.OpenTextFile(myCSV, 2, true, 0)

objOutputFile.Write strNewLineContent
objOutputFile.Close

The resulting CSV looks like the below:

Mr AB Sample1|1 Sample Lane|Sample|County Sample|AA1 1AA|UK|11x17Landscape.pdf|17|11
Mr AB Sample2|2 Sample Lane|Sample|County Sample|AA2 2AA|UK|8.5x11Portrait.pdf|8.26|11.69
Mr AB Sample3|3 Sample Lane|Sample|County Sample|AA3 3AA|UK|11x17Landscape.pdf|17|11
Mr AB Sample4|4 Sample Lane|Sample|County Sample|AA4 4AA|UK|11x17Landscape.pdf|17|11
Mr AB Sample5|5 Sample Lane|Sample|County Sample|AA5 5AA|UK|8.5x11Portrait.pdf|8.26|11.69

Thanks Rod

I was hoping to be able to do it in the template itself but this approach should work fine

Perhaps there is a way to achieve this in the template itself but…adding more scripts to the template which can potentialy open, read\write thousands of PDF files from disk or network, will only slow down Content Creation.

In the Designer this could be achieved using the resource() command:

var pdf = resource("images/stamp.pdf");
var height = pdf.height;
var width = pdf.width;
var numberOfPages = pdf.pages;

It can also be used to check if the file exists:

if(resource("file://C:/paw.pdf")){
   //exists
} else {
   //oops
}
1 Like

This doesn’t seem to work for me in a control script. I get a “Unknown protocol: about” error on the line where I am using the resource() command; the script just fails and so I can’t use it to enable the right print section. Resource() does work fine in a normal script - I can return the page height or width for example.

I was able to resolve the “Unknown protocol: about” error by removing the “file://” from the path that is passed to resource().

Example:

The following results in “Unknown protocol: about” error:

var pdf = resource([file://C:/_TEMP/Form.pdf](file://C:/_TEMP/Form.pdf));

The following does not result in “Unknown protocol: about” error:

var pdf = resource(“C:/_TEMP/Form.pdf”);

Erik,

I used your idea to create this control script. The notion is to create a one page pdf using the variables from a post and then pull in the attachment which is either a pdf or a tiff file as the background. I’m hoping that all of the pages in a multi-page pdf or multi-page tiff will be added to the document. The eventual document will be rendered as a PDF unless there is a way to output a multi-page tiff file.

//Set the background url
var resourceUrl = ‘images/’ + record.fields.original;

//Collect the width and height
var pdf = resource(‘images/’ + record.fields.original);
var height = pdf.height;
var width = pdf.width;

// Initially disable output for the Print sections
merge.template.contexts.PRINT.sections[‘AttachmentP’].enabled = false;
merge.template.contexts.PRINT.sections[‘AttachmentL’].enabled = false;

// Enable a print section based on the width portrait vs landscape
if(width >= ‘650’) {
merge.template.contexts.PRINT.sections[‘AttachmentL’].enabled = true;
merge.template.contexts.PRINT.sections[‘AttachmentL’].background.url = resourceUrl;
} else {
merge.template.contexts.PRINT.sections[‘AttachmentP’].enabled = true;
merge.template.contexts.PRINT.sections[‘AttachmentP’].background.url = resourceUrl;
}

I also had to remove the “File://” to get it to work.

var infile = ‘C:/out/’ + record.fields.original;

// Initially disable output for the Print sections
merge.template.contexts.PRINT.sections[‘AttachmentP’].enabled = false;
merge.template.contexts.PRINT.sections[‘AttachmentL’].enabled = false;

if(resource(infile)){
//Set the background url
var resourceUrl = infile;
//Collect the width and height
var pdf = resource(infile);
var height = pdf.height;
var width = pdf.width;
var numberOfPages = pdf.pages;

// Enable a print section based on the width portrait vs landscape
if(width >= ‘650’) {
merge.template.contexts.PRINT.sections[‘AttachmentL’].enabled = true;
merge.template.contexts.PRINT.sections[‘AttachmentL’].background.url = resourceUrl;
} else {
merge.template.contexts.PRINT.sections[‘AttachmentP’].enabled = true;
merge.template.contexts.PRINT.sections[‘AttachmentP’].background.url = resourceUrl;
}

} else {
//oops
}