Using cfpdf to display pdf files - coldfusion

I have a page where I need to display a pdf and an "I agree" button.
Q: Do I use cfpdf to create a thumbnail of the pdf and display the resulting output?
Chapter 32 of cfWack 8 talks about using cfpdf.

I did this in the office once...It's not that sweet as it's presented in Adobe's whitepapers.
Here's my code:
<cfpdf source="#attachmentFilePath##attachmentFilename#" pages="1" imagePrefix="#prefix#" action="thumbnail" destination="#application.attachmentsFilePath#/_thumbs" format="jpg" overwrite="true" resolution="low" scale="80">
<cfset thumbURL = "http://127.0.0.1:9001/attachments/_thumbs/#prefix#_page_1.jpg">
<cfif fileExists(thumb)>
<tr>
<td width="100%">
<a href="attachmentView.cfm?attachNo=#attachNo#&act=download">
<img alt="#attachmentFilename#" src="#thumbURL#" width="500" />
</a>
</td>
</tr>
<cfelse>
<cfheader name="Content-Type" value="#attachmentMIMEType#">
<cfheader name="Content-Disposition" value="attachment; filename=#attachmentFilename#">
<cfcontent type="#attachmentMIMEType#" file="#attachmentFilePath##attachmentFilename#">
</cfif>
I also check isPDF() and putting all this in try/catch.
So what's wrong with this? It works with 30% of PDF users are trying to preview so in catch I display link for download instead of image :(

Related

How to store an image file after uploading the image file in ColdFusion

I am fairly new to Coldfusion and I am having a hard time to figure out how to allow users to upload images in my form
Currently, I was able to find the following code that will upload the image in Coldfusion:
<cfparam name="form.fileUpload" default="">
​
<cfif len(trim(form.fileUpload))>
<cffile action="upload"
fileField="fileUpload"
destination="C:\docs">
<p>Thankyou, your file has been uploaded.</p>
</cfif>
​
<form enctype="multipart/form-data" method="post">
<input type="file" name="fileUpload" /><br />
<input type="submit" value="Upload File" />
</form>
Although it gives me an idea on how to approach my issue, I still not sure on the following:
Instead of the destination="C:\docs" storing the file at a drive, I would like to be able to upload the uploaded image to an email. The reason being is the once the user finishes and submits the form, an email will be sent out to the user who submitted the request and the user who will be assigned in creating the card.
How can I achieve this? Any suggestions and examples would be greatly appreciated
Use CFMAILPARAM with remove="yes". You can also show the filename in the email. Full example:
<cfmail
to="the#recipient.com"
subject="#application.companyName# Contact Submission"
type="html"
from="#form.email#">
<!--- WAS A FILE UPLOADED? --->
<cfif isDefined("form.attachment") and form.attachment neq ''>
<!--- SAVE THE FILE --->
<cffile action="upload" fileField="attachment" destination="#expandPath('./')#" nameConflict="Overwrite">
<!--- ATTACH TO EMAIL, THEN DELETE IT --->
<cfmailparam
disposition = "attachment"
file = "#expandPath('./' & cffile.serverfile)#"
remove = 'yes'>
<!--- MAKE THE FILE NAME A FORM FIELD --->
<cfset form.attachment = cffile.serverfile>
</cfif>
<html>
<body>
<cfset form.URL = cgi.http_referrer>
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<th>Field</th>
<th>Value</th>
</tr>
<cfloop list="#form.fieldnames#" index="f">
<tr>
<td nowrap="nowrap" valign="top">#replace(f, '_', ' ', 'all')#</td>
<td>#form[f]#</td>
</tr>
</cfloop>
</table>
</body>
</html>
</cfmail>

Coldfusion creating reports with wkhtmltopdf

I am trying to get some assistance with wkhtmltopdf.
I have downloaded and installed and trying to get it to work correctly with my program.
What I have is a form that lets the user choose print, preview or excel. When the user chooses print I want pop up the html table that is created by a query depending on the associates and locations that are chosen.
Right now when the user chooses print it just shows this table in the browser.
<cfif FORM.Format IS "print">
<!---<cfdocument format="pdf" scale="75" backgroundvisible="yes" overwrite="no" fontembed="yes">--->
<link rel="stylesheet" href="css/form-table.css"/>
<!---<cfdocumentitem type="header" >
<cfoutput><p align= "right">Page #cfdocument.currentpagenumber# of #cfdocument.totalpagecount#</p></cfoutput>
</cfdocumentitem> --->
<div class="RTable">
<h3 class="RTable-h3">CHECKLIST STATS</h3>
<cfoutput>
<ul class="RTable-headingList">
<li>FROM <span class="RTable-headingList-date">#dateFormat(date1, 'mm/dd/yyyy')#</span> TO <span class="RTable-headingList-date">#dateFormat(date2, 'mm/dd/yyyy')#</span></li>
<li>LOCATIONS: <span class="RTable-headingList-locations">#locList#</span></li>
</ul>
</cfoutput>
<table class="table table-hover RTable-table">
<thead>
<tr>
<th>Associate Name</th>
<th>Location</th>
<th><small>Generated by</small>Associate</th>
<th><small>Generated by</small>Selected Location(s)</th>
<th><small>Associate Percentage of</small>Location Total</th>
</tr>
</thead>
<tbody>
<cfoutput query="GetEmployeeInfo">
<tr>
<td class="RTable-name"><cfif rnA EQ 1><strong>#assoc_name#</strong></cfif></td>
<td class="RTable-location"><cfif rnL EQ 1>#trans_location#</cfif></td>
<td>#checklistsByAssocLoc#</td>
<td>#assocChecklistsByLoc#</td>
<td>#DecimalFormat(totalChecklistsByAssocLocPct)# %</td>
<!---<td> rnA: #rnA# | rnL: #rnL# | rnTotAssoc: #rnTotAssoc# </td> --->
</tr>
<cfif rnTotAssoc EQ 1>
<tr class="RTable-row-associate-total">
<td>Associate Total</td>
<td></td>
<td>#totalChecklistsByAssoc#</td>
<td>#totalAssocChecklistsByAllFilteredLoc#</td>
<td>#DecimalFormat(totalChecklistsByLocPct)# %</td>
</tr>
</cfif>
</cfoutput>
</tbody>
</table>
</div>
<!---</cfdocument>--->
I am trying to use it like cfdocument in the <cfif FORM.Format IS "print"> do I cfexecute this table some how in replace of how I have the table? I am using this as reports and dont want to save a million reports to the server. I guess I am looking for some assistance in getting off on the right foot. Any help would be greatly appreciated.
Iv tried adding this code inside the if "print":
<cfexecute name="C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"
arguments="http://path/path/checklist/wkhtmltestpage.cfm C:\temp\wkhtmlTest.pdf"
errorVariable="errorMessage"
timeout="10"
/>
But this does not download it to show the user to print.
Also for some reason its not going to the link I am specifying its making a pdf of the login page...
When WKHTMLTOPDF requests the webpage, the "user" is not authenticated and the CGI.Http_User_Agent contains "wkhtmltopdf". The remote IP of the request will also be a local IP that is configured on the server. As a result, I don't directly process any CFM scripts using WKHTMLTOPDF.
I recommend:
Use CFContent to capture generated static HTML.
Save the HTML to a public-accessible directory with a random file name.
Use the web path + random file name w/WKHTMLTOPDF command line.
After PDF is generated, delete random files.
This process will make any issues easier to troubleshoot. I prefer creating static BAT files with the same randomized filename so that I can manually re-run on the server or even locally on my on PC to further troubleshoot and view any weird messages returned by the program. There's many other command line settings you'll want to pass (margins, orientation, pagesize, header/footer, JS delay, etc), so creating a standalone BAT file with all of the command line arguments is the best approach. (I noticed in another question that you may be using CFX_Exec. I recommend using it over CFExecute.)
<!--- Simple WKHTMLTOPDF Generation Usage --->
<cfset FileID = CreateUUID()>
<cffile action="WRITE" file="#webroot#\#FileID#.htm" output="#TheHTML#">
<cfexecute name="C:\wkhtmltopdf.exe" arguments="http://mywebsite.com/#FileID#.htm C:\temp\#FileID#.pdf" timeout="30">
<cffile action="DELETE" file="#webroot#\#FileID#.htm">
To deliver the PDF file, you can either perform a 302 redirect to the randomized PDF file and let your webserver handle the mimetype (and then delete it later) or use CFContent to deliver it using a ColdFusion thread and automatically delete it:
<!--- Return PDF to browser inline or as downloadable attachment --->
<!--- <cfheader name="content-disposition" value="attachment; filename=""NiceFilename.pdf"""> --->
<cfheader name="content-disposition" value="inline; filename=""NiceFilename.pdf""">
<cfcontent type="application/pdf" file="C:\temp\#FileID#.pdf" deletefile="Yes">

ColdFusion - CFDOCUMENT Title in URL

I am creating a PDF document using ColdFusion cfdocument tag. Works fine, however instead of showing the document name in browser Title - it shows the .cfc file that I call to create the PDF.
Here is how I'm calling it.
<cfdocument format="pdf" marginbottom=".5" margintop=".25" marginright=".5" marginleft=".5" saveAsName="#filename#.pdf">
<cfdocumentitem type="footer">
<p style="font-size:11px; text-align:right; font-style:italic;">Page #cfdocument.currentpagenumber# of #cfdocument.totalpagecount#</p>
</cfdocumentitem>
<html>
<head><title>#filename#.pdf</title></head>
<body><img src="file:///#application.tempFolder#\#thisFilename#" /></body>
</html>
</cfdocument>
What the heck am I missing? Why does it still show the filename.cfc file that I'm calling in the browser title instead of the filename I give to the PDF???
Figured it out. Had to create the document using CFDOCUMENT, then add a "Title" attribute to it using the CFPDF tag. Then output it to the browser.
<!--- Create the PDF --->
<cfdocument format="pdf" marginbottom=".5" margintop=".25" marginright=".5" marginleft=".5" filename="#application.tempFolder#\#thisSaveAsFilename#" overwrite="yes">
<cfdocumentitem type="footer">
<p style="font-size:11px; text-align:right; font-style:italic;">Page #cfdocument.currentpagenumber# of #cfdocument.totalpagecount#</p>
</cfdocumentitem>
<html>
<head><title>#thisSaveAsFilename#</title></head>
<body><img src="file:///#application.tempFolder#\#thisFilename#" /></body>
</html>
</cfdocument>
<!--- Use CFPDF to add attributes to it --->
<cfset thisInfo = StructNew()>
<cfset thisInfo.Title = "pdf title goes here...">
<cfpdf action="setinfo" info="#thisInfo#" source="#application.tempFolder#\#thisSaveAsFilename#" />
<!--- Send it to the browser --->
<cfcontent file="#application.tempFolder#\#thisSaveAsFilename#" type="application/pdf" />A

CFDocument Images

I am having a problem displaying images within cfdocument. I have tried several different options without luck (please see test code below).
It seems that cfdocument recognizes there is an image file there, because the size of the table cell changes. Yet still it doesn't display content of the image.
I would appreciate any help.
<cfdocument format="PDF" localURL = "yes">
<p style="background-color:#006633">
<table border="1">
<tr><td><cfoutput>
output from "file:///#ExpandPath('CEUCertificate_ABondy3.jpg')#"
</cfoutput>
</td>
<td style="background-color:white">
<cfoutput>
<image src="file:///#ExpandPath('CEUCertificate_ABondy3.jpg')#">
</cfoutput>
</td>
</tr>
<tr><td><cfoutput> output from #ExpandPath('CEUCertificate_ABondy3.jpg')# </cfoutput> </td>
<td>
<cfoutput>
<image src="#ExpandPath('CEUCertificate_ABondy3.jpg')#">
</cfoutput>
</td>
</tr>
<tr><td style="background-color:red">
Output from: img src="CEUCertificate_ABondy3.jpg"
</td>
<td style="background-color:red">
<img src="CEUCertificate_ABondy3.jpg"/>
</td>
</tr>
<tr>
<td style="background-color:white">
output from image src="CEUCertificate_ABondy3.jpg"
</td>
<td>
<cfoutput>
<image src="CEUCertificate_ABondy3.jpg">
</cfoutput>
</tr>
<tr>
<td>Output from local url</td>
<td>
<cfoutput>
<img src=#localUrl("CEUCertificate_ABondy3.jpg")#>
</cfoutput>
</td>
</tr>
</table>
</p>
</cfdocument>
<cffunction name="localUrl" >
<cfargument name="file" />
<cfset var fpath = ExpandPath(file)>
<cfset var f="">
<cfset f = createObject("java", "java.io.File")>
<cfset f.init(fpath)>
<cfreturn f.toUrl().toString()>
</cffunction>
For your initial question, you need to reference the images in your cfdocument tag as you would with any other HTML page; <img src="url-to-the-image" />. So this should work for you.
<img src="/cde/mobileweb/CEUCertificate_ABondy3.jpg"/>
Now that you have made that change the page is timing out. This is probably due to how the image was created. See this page here. Which I found from Charlie's page here.
Try opening that image in an editor and re-saving it. Then see if the processing time decreases.
I found some more discussion about this on Ben Nadel's blog here.
Instead of messing with ExpandPath() and file://, code it as a normal html and make sure it works first. Then use localUrl="true" in your <cfdocument>

Creating a Word document in Coldfusion - how to have pagenumbering?

I am creating a Word format .doc using the following code, then cfheader and cfcontent to serve. All is good but I need to be able to place dynamic information in the header (or footer), or automatic pagenumbering would be a second best option.
How should I modify the code?
<cfsavecontent variable="myDocument">
<html xmlns:w="urn:schemas-microsoft-com:office:word">
<!--- Head tag instructs Word to start up a certain way, specifically in
print view. --->
<head>
<xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:SpellingState>Clean</w:SpellingState>
<w:GrammarState>Clean</w:GrammarState>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
</w:Compatibility>
<w:DoNotOptimizeForBrowser/>
</w:WordDocument>
</xml>
</head>
<body>
Regular HTML document goes here
<!--- Create a page break microsoft style (took hours to find this)
--->
<br clear="all"
style="page-break-before:always;mso-break-type:page-break" />
Next page goes here
</body>
</html>
</cfsavecontent>
Please have a look at this: Header & Footer
I have successfully created custom header and footer with only one html file using this article. (Word 2003)
Hope this helps!
Doesn't seem easy to add page number using a WordprocessingML
http://openxmldeveloper.org/archive/2006/08/03/443.aspx
If you can serve PDF instead of DOC, here's a solution for page numbering.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7c21.html
See example 2:
<cfdocument format="pdf">
<cfdocumentitem type="header" evalatprint="true">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td align="right"><cfoutput>#cfdocument.currentsectionpagenumber# of
#cfdocument.totalsectionpagecount#</cfoutput></td></tr>
</table>
</cfdocumentitem>
<cfdocumentitem type="footer" evalatprint="true">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td align="center"><cfoutput>#cfdocument.currentpagenumber# of
#cfdocument.totalpagecount#</cfoutput></td></tr>
</table>
</cfdocumentitem>
...
</cfdocument>