Copy link to clipboard
Copied
I have a ColdFusion script that creates a PDF to be sent for archiving. The PDF is created with the following function:
Copy link to clipboard
Copied
Good news/bad news. First just add this as an attribute on your cfdocument line:
localurl="true"
That should solve it. Why? Why is this necessary?
What you're suffering is neither a bug nor is it new behavior. This "solution" is also quite old. FWIW, I have a 2011 blog post on the matter:
https://www.carehart.org/blog/2011/11/19/perf_fix_via_cfdocument_localurl_attribute
As I note there, this attribute can also solve critical performance problems with cfdocument, arising from its underlying behavior in discuss there.
And it's certainly frustrating that we should even have to worry about this, but it is what it is. The attribute and issue is also documented, of course. But this is one of those things that bites people who are unaware.
Let us know if this solves things for you.
Copy link to clipboard
Copied
I suspect that the cause of the problem is that, given the relative path, ColdFusion is sometimes unable to resolve the full path to the image file. I would solve this by simplifying the structure of the code.
The simplification steps I would follow are:
Step 1: Simplify the code of the checkImage() function so that it does just one thing: check for file existence. Given the relative path of an image file, it checks whether the file exists and whether it is a valid image.
<cffunction name="checkImage" returntype="string">
<cfargument name="img_relative_path" type="string"><!--- Image relative path --->
<!--- Obtain absolute path of image file from relative path --->
<cfset var img_full_path = expandPath("#arguments.img_relative_path#")>
<!--- Test if the file exists and if it is an 'image'. If it is, return the full path. If it isn't, return the empty string --->
<cfif fileExists(img_full_path) and isImageFile(img_full_path)>
<cfreturn img_full_path>
<cfelse>
<cfreturn "">
</cfif>
</cffunction>
Step2: Store the code of the checkImage() function as the page checkImg.cfm. This code is related to images, so store the CFM page within the img directory. The page is then located at ../../portale_admin/img/checkImg.cfm.
From now on, wherever you need to check for the existence of an image, you can just include the page checkImg.cfm. When you do, ensure that the relative paths are accurate.
Step 3: Check for the existence of the image. If it exists, use it to generate the PDF.
<!--- The CFM page that generates the PDF document. --->
<!--- Make sure the relative paths are correct. --->
<cfset imageFile="../../portale_admin/img/image.png">
<cfinclude template="../../portale_admin/img/checkImg.cfm" ><!--- CFM containing the function --->
<cfif checkImage(imageFile) is not "">
<!--- Generate the PDF, inserting the image into it --->
<cfdocument format="pdf" orientation="portrait" pagetype="A4" unit="cm" marginbottom="2" margintop="2" filename ="#path#" overwrite="true">
<p style="font-size:9px;">
<cfoutput> <img src="#imageFile#" align="#positioning#"></img></cfoutput>
</p>
</cfdocument>
<cfelse>
<cflocation url="/missioni/portale_admin/no_img_pdf.cfm">
</cfif>
Copy link to clipboard
Copied
All that said, I sure hope just adding the localurl attribute will suffice for domeniko. 🙂
Copy link to clipboard
Copied
According to @domeniko_86 , "... 99% of the time, it is correctly included.". That is the main reason why I suggested a sustainable design solution.
Nevertheless, it will in fact be an advantage to combine both solutions.
Copy link to clipboard
Copied
I still stand by my answer. It could entirely and easily solve the problem, as it changes how cfdocument works for local images. And he did say that "the image exists because it is static and present in the file system".
As for your focus, we don't know if that 99% refers to "calls to a given file" or "calls across many different ones" (where perhaps some always fail, but it's only 1% of his attempts).
Further, and though I didn't want to distract from that key solution, the op's idea (and your elaboration) of doing a cfimage to test whether "the image is fine" is flawed, in that cfimage WILL use that file path to find it--without doing the underlying http call, which cfdocument does unexpectedly by default even to find a local image.
I didn't elaborate in my first reply on that (the+at crux of the issue with cfdocument) in my first reply. Perhaps I should have. I was trying to keep it a relatively short reply and the blog post I pointed to did explain that. I hoped domeniko would simply try it and perhaps report quick success. 🙂
Let's see now what he finds, from either solution.
Copy link to clipboard
Copied
Cfdocument isn't working alone. There is a function involved.
In any case, though our suggestions are different, the one does not preclude the other.
Copy link to clipboard
Copied
... Further, and though I didn't want to distract from that key solution, the op's idea (and your elaboration) of doing a cfimage to test whether "the image is fine" is flawed, in that cfimage WILL use that file path to find it--without doing the underlying http call, which cfdocument does unexpectedly by default even to find a local image. ...
By @Charlie Arehart
To be clear, my suggestion does not need cfimage to read the image.
Copy link to clipboard
Copied
@domeniko_86 can you please let us know how things turned out? Were you able to resolve things with either of the first two suggested solutions?