CFScript PDF Merge with Multiple Sources

1.2k views Asked by At

I'm trying to do a PDF merge in CFScript.

So far I have:

pdfService = new pdf();
pdfService.addParam(source='#source1#');
pdfService.addParam(source='#source2#');
writedump(pdfService);
pdfService.merge(destination="#getTempDirectory()#myfile.pdf";

But seems like the addParam is not working. When the pdfService is dumped after the addParams, the source is not set. What is the correct way of setting it using addParams? If there is only one source then it works fine by using

pdfService.addSource(source1);

But in the case of multiple sources, the addParam is not working and when it tries to do the merge an error comes up saying that the source is empty.

Note that the source is not a path in the file system, it is a binary PDF object. It works fine when I do it in the usual CF way:

<cfpdf action="merge" destination="#getTempDirectory()#myfile.pdf">
    <cfpdfparam source="source1" />
    <cfpdfparam source="source2" />
</cfpdf>

But I need to get it to work in CFScript.

1

There are 1 answers

2
CfSimplicity On

It might be to do with the way you're reading in the sources (your code doesn't show that). Try the following which works on CF9.01 and assumes the pdf files are in the same directory as the script (EDIT: source PDFs are read from URLs):

pdfService  =   New com.adobe.coldfusion.pdf();
//source1   =   pdfService.read( source="#ExpandPath( 'a.pdf' )#",name="source1" );
//source2   =   pdfService.read( source="#ExpandPath( 'b.pdf' )#",name="source2" );
source1 =   pdfService.read( source="http://www.mysite.com/viewpdf/a.pdf",name="source1" );
source2 =   pdfService.read( source="http://www.mysite.com/viewpdf/b.pdf",name="source2" );
pdfService.addParam( source="source1" );
pdfService.addParam( source="source2" );
pdfService.merge( destination="#ExpandPath( 'merged.pdf' )#" );

Incidentally I'd recommend always specifying the full dot-path to the CF script "tags as cfcs", in case you have any similarly named components anywhere else (i.e. New com.adobe.coldfusion.pdf() rather than just New pdf()).