Skip to main content
New Participant
February 13, 2025
Question

No image on pdf document

  • February 13, 2025
  • 3 replies
  • 770 views

I have a ColdFusion script that creates a PDF to be sent for archiving. The PDF is created with the following function:

<cfdocument format="pdf" orientation="portrait" pagetype="A4" unit="cm" marginbottom="2" margintop="2" filename ="#path#" overwrite="true">
The problem is that sometimes the generated file is missing images and instead shows a square with a red 'x' inside. The issue is that the image exists because it is static and present in the filesystem:
<img src="../../portale_admin/img/image.png" align="#positioning#"></img>
And 99% of the time, it is correctly included. In addition i have a function:
<cffunction  name="checkImage" returntype="void">
  <cfargument  name="img_path">
  <cfset img_path = ExpandPath("#img_path#")>
  <cftry>
      <cfimage action="read" source="#img_path#" name="testImage">
  <cfcatch>
      <cflocation  url="/missioni/portale_admin/no_img_pdf.cfm">
  </cfcatch>
  </cftry>
</cffunction>

<p style="font-size:9px;">
                <cfset checkImg = checkImage("../../portale_admin/img/image.png")>
                <img src="../../portale_admin/img/image.png" align="#positioning#"></img>
</p>







 


    3 replies

    Charlie Arehart
    Community Expert
    Community Expert
    March 7, 2025

    @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? 

    /Charlie (troubleshooter, carehart. org)
    BKBK
    Community Expert
    Community Expert
    February 13, 2025

    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>

     

     

     

     

    Charlie Arehart
    Community Expert
    Community Expert
    February 13, 2025

    All that said, I sure hope just adding the localurl attribute will suffice for domeniko. 🙂 

    /Charlie (troubleshooter, carehart. org)
    BKBK
    Community Expert
    Community Expert
    February 13, 2025

    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.

    Charlie Arehart
    Community Expert
    Community Expert
    February 13, 2025

    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. 

    /Charlie (troubleshooter, carehart. org)