Boy, I'm just Mr. Looking-for-answers today, aren't I?
We have a need to generate a simple PDF form that contains dynamic
data; it's a PDF that will be sent to clients with their contact
information in it, and we want them to check what we have and make
corrections where needed. Obviously, the contact info will be coming
from a database.
Knowing that expensive solutions exist, but not having the budget for
that, I thought about generating a PDF form, place unique strings as
the field values (i.e. $name$ for where the name data will go); I
could then read in the PDF "template", replace the unique strings with
query values, re-save the PDF as a new file, and I'd be golden.
Alas, no. When I try to read in the PDF using cffile action="read",
the generated PDF loads but pops an error and when I open it with a
text editor, the original strings are still there. When I use
"readbinary" and then convert the output tobase64(), it generates the
PDF file just fine but the original (i.e. $name$) values are still
there. I've tried a number of permutations but have gotten nowhere
with this. Has anyone done this successfully, and if so, what did you
do?
Thanks,
Pete
Here's my code:
<cfoutput>
<cfparam name="a" default="">
<cfswitch expression="#a#">
<cfcase value="merge">
<!--- Read original file (sample_form.pdf) and set as a
variable --->
<cffile action="READ" file="#expandpath("sample_form.pdf")#"
variable="pdf">
<cfset pdf = tobase64(pdf)>
<cfdump var="#pdf#">
<!--- Replace values in PDF with values from submitted form --->
<cfset pdf = replace(pdf, "$name$", FORM.fullname, "ALL")>
<cfset pdf = replace(pdf, "$email$", FORM.email, "ALL")>
<cfset pdf = replace(pdf, "$hiddenvalue$", FORM.hiddenvalue,
"ALL")>
<cfset pdf = replace(pdf, "$actionurl$", FORM.actionurl, "ALL")>
<cfset pdf = replace(pdf, "$submit_to_email$",
FORM.submit_to_email, "ALL")>
<!--- Convert back to binary --->
<cfset pdf = tobinary(pdf)>
<!--- Save PDF as a new file --->
<cfset tmp_path = ExpandPath("*.*")>
<cfset tmp_path = GetDirectoryFromPath(tmp_path)>
<cffile action="write" file="#tmp_path##FORM.fullname#.pdf"
output="#pdf#" addnewline="no" fixnewline="no">
<!--- Redirect user to display PDF --->
<meta http-equiv="refresh" content="0; url=#FORM.fullname#.pdf">
</cfcase>
<cfcase value="show"></cfcase>
<cfdefaultcase>
<form action="#CGI.script_name#" method="post">
<input type="hidden" name="a" value="merge">
Name: <input type="text" name="fullname"><br>
Email: <input type="text" name="email"><br>
Hidden Value: <input type="text" name="hiddenvalue"><br>
Action URL: <input type="text" name="actionurl"><br>
Submit to email: <input type="text"
name="submit_to_email"><br>
<button type="submit">Submit</button>
</form>
#expandpath("sample_form.pdf")#
</cfdefaultcase>
</cfswitch>
</cfoutput>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:236408
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54