Link to home
Start Free TrialLog in
Avatar of erikTsomik
erikTsomikFlag for United States of America

asked on

Multiple cfcontent downloads via query

Create multiple PDF using coldfusion and cfcontent. I have the CF query and I need for each person generate the PDF file. The way I do is cfoutput through the query into the variable and then put that variable into cfdocument and then use <cfcontent to produce PDF through the browser. But the broser only producing the PDF for 1 person . I think this is because cfcontent but how can i produce as many PDFs  as people produced by the query  

<cfoutput query="rc.getResultByCompetency" group="rateeID">
    	<cfsavecontent variable="output">
               output here
       </cfsavecontent>
<cfdocument
						name="certificate_#rateeID#"
						orientation ="portrait"
						mimetype="application/pdf" overwrite="true" 
						format="PDF">
					#output#
				</cfdocument>		
	<cfset test = evaluate("certificate_#rateeID#")>
				<cfheader name="content-disposition" value="attachment;filename=#rc.getResultByCompetency.rateeName#.pdf">
				<cfcontent type="application/pdf" variable="#toBinary(test)#" >	
 </cfoutput>

Open in new window

Avatar of erikTsomik
erikTsomik
Flag of United States of America image

ASKER

any suggestions
Avatar of _agx_
Frankly, HTTP isn't designed to do that. This thread explains it well:

HTTP is a request/response system. Any response has to come as a reply to a request. Given that, you can't send multiple responses to a single request. If nothing else, there would be no client listening for those responses (because it already got the response it was waiting for).

So essentially you have two options:

    1. Issue multiple requests, one for each "file" being downloaded. This will create multiple responses for the client to expect.
    2. Combine the files into a single file using some archiving tool (Zip libraries are pretty standard for this)
    ....

There are hacks which claim to support this, such as using iframes or jquery plugins, but it involves more complexity and browser support is iffy.  If you must generate individual pdf's. the best option IMO is to Zip the pdfs, and return a single .zip file.
I need to return individual files but I like the idea of putting everything into a zip file. How can I do that in ColdFusion
Create a directory to store the files. The directory name must be unique to avoid conflicts

<!--- create temp directory to store files --->
<cfset tempDir = ExpandPath("./#createUUID()#")>
<cfset DirectoryCreate(tempDir)>

Open in new window


Generate the pdf's and save them to the temp directory

<!--- create first file --->
<cfdocument filename="#tempDir#/myFirstFile.pdf" format="pdf">First file</cfdocument>
<!--- create second file --->
<cfdocument filename="#tempDir#/mySecondFile.pdf" format="pdf">second file</cfdocument>

Open in new window


Then create a zip file containing the pdf's. The file name must be unique to avoid conflicts

<cfset zipFilePath = ExpandPath('./#createUUID()#.zip')>	
<cfzip file="#zipFilePath#" source="#tempDir#">

Open in new window


Download the zip file as an attachment. Change "filename=...." to whatever name you want to display

<cfheader name="Content-Disposition" value="attachment; filename=nameYouWantToDisplay.zip">
<cfcontent type="application/zip" file="#zipFilePath#" deleteFile="yes">

Open in new window


Cleanup and remove the temp directory

<cfset DirectoryDelete(tempDir)>

Open in new window

Awesome job . One more question . I am trying to show a progress bar but it works in HTML and it shows no image in PDF

<div class="rateBarDIV" style="background-color: ##D3D3D3;">
                                                                              <img src="/includes/images/pixelDeepOrange.png" style="vertical-align: top;" height="21" width="#percent#%" />
                                                                        </div>
Maybe try adding localUrl = "yes" to <cfdocument>?
when localUrl  is added it makes zip file generation so long and the image is still not ahing

<div class="rateBarDIV" style="background-color: ##D3D3D3;">
                                                                              <img src="#ExpandPath('/includes/images/pixelDeepOrange.png')#" style="vertical-align: top;" height="14" width="#percent#%" />
                                                                        </div>
It shouldn't affect the zip generation, though maybe the pdf part.  Unfortunately, I'm not near a CF install right now. Since this is a new issue, I'd suggest opening a new thread and hopefully someone else like GD will chime in on it...
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial