Question

I have multiple pdfs stored in my database as blobs and I need to merge them to create a single pdf that needs to be streamed to the user.

I understand that it is fairly easy to do this if I am rendering a single pdf from blob, but I cannot figure how to merge multiple blobs.

<cfheader name="Content-Disposition" value="inline; filename=#document.name#.#document.ext#">
<cfcontent type="application/pdf" variable="#document.content#">

I see that CFPDF helps with this functionality, but cant seem to be able to get my blob into a cfpdf variable. A similar question has been asked here before, but it doesnt have the answer I seek.

Thanks!

Was it helpful?

Solution

Try converting the blob data to a file (in-memory using ram:// if possible to save writing to disk) and then use that as the cfpdf merge source. You can do it for each blob as you loop over your query within the cfpdf action="merge" tag:

<cfquery name="q" datasource="test">
  SELECT content FROM pdfs
</cfquery>

<cfpdf action="merge" name="mergedPdf">
  <cfloop query="q">
    <cfset tempBinary=q.content><!---intermediate var seems to be necessary in some environments --->
    <cffile action="write" output="#tempBinary#" file="ram://temp.pdf">
    <cfpdfparam source="ram://temp.pdf">
  </cfloop> 
</cfpdf>
<cfcontent type="application/pdf" variable="#ToBinary( mergedPdf )#" reset="true">

Note you can use a single temp file - no need to create a different one for each blob in the query.

OTHER TIPS

I think you can do the following

Retrieve each pdf blob stored in db and create pdf using cfpdf and store them in some temp directory

  <cfpdf action="write" destination="c:\pdfs\1.pdf" source="#mypdfblob1" >

Retrieve all such blobs and store them as pdfs in the temp directory

Merge all those pdfs using cfpdf merge by specifying the temp directory in cfpdf merge

<cfpdf action="merge" directory="C:\pdfs" destination="C:\result.pdf">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top