1

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!

Community
  • 1
  • 1
Helius 06
  • 87
  • 1
  • 12
  • Also tried http://stackoverflow.com/questions/10690548/coldfusion-cfpdf-reading-a-binary-database-column/10692761#10692761 but am getting the error: An error occurred during the EXTRACTTEXT operation in the cfpdf tag. Error: Invalid Document. Check your source or files in the directory. – Helius 06 Jul 28 '14 at 18:06

2 Answers2

1

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.

CfSimplicity
  • 2,338
  • 15
  • 17
  • CfSimplicity, Thanks for this answer. I had to come back to this after a while and figured I hadnt solved this issue. You method worked, except for a minor correction. I have edited the code to include the update. – Helius 06 Oct 10 '14 at 17:07
  • Glad to help. My original code works on CF9.0.1 without the intermediate variable for `q.content`. Which CF version are you using? – CfSimplicity Oct 10 '14 at 17:48
  • I am on cf 9.0.1 as well. I was getting an "error during MERGE" without the intermediate variable. – Helius 06 Oct 10 '14 at 18:49
  • Strange. Perhaps a difference in HotFixes. Anyway the intermediate var also works in my environment so I've adjusted my code in case others have the same issue. – CfSimplicity Oct 10 '14 at 19:06
0

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">
Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82