1

curious one this.

I'm working on a process that generates PDF files, combining data from various sources. The last piece of this process I need to complete is merging in image files.

This is actually fairly straightforward but the problem I have is the image files aren't stored with file extensions. Locally, I can change the filename, but in production this isn't an option.

So because a filename looks like : B71637CB-A49C-0653-EF813918736BDEB7

This will not work:

<cfimage action="writeTobrowser" source="#FilePath#> 

Same with

<img src="#FilePath#">.

So, any ideas on how I can work around this? Here's the code in context:

<cfdocument format="PDF" name="report" filename="#fileToDownloadimage#" overwrite="yes">
    <cfdocumentsection>
        <cfimage action="writeTobrowser" source="#FilePath#.jpg">
    </cfdocumentsection>
</cfdocument>
  • WriteToBrowser just generates an html `` tag. Try using a plain `` tag instead of cfimage. – Leigh Dec 16 '15 at 16:52
  • Just to add to this, I've experienced a similar problem before. Solution is incredibly easy. In ColdFusion 10 and beyond there exists a function called FileGetMimeType which reads the file's actual contents to determine its extension. https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-e-g/filegetmimetype.html – TRose Dec 16 '15 at 22:34

3 Answers3

3

So here's what ended up working:

<cfdocument format="PDF" name="report" filename="#fileToDownloadimage#" overwrite="yes">
    <cfdocumentsection>
        <cfset fileObject = fileReadBinary('#FilePath#') />
        <cfset imageObject = imageNew(fileObject) />
        <cfimage action="writeTobrowser" source="#imageObject#">
    </cfdocumentsection>
</cfdocument>

Alex's answer got me down the right path so I'm perfectly happy to leave the kudos in place, cos I wasn't getting anywhere near this!

1

If you need to embed the images into the PDF document, try HTML's inline image capabilities:

<cfset fileLocation         = "/path/to/images/B71637CB-A49C-0653-EF813918736BDEB7">
<cfset imageContent         = fileReadBinary(fileLocation)>
<cfset imageContentAsBase64 = toBase64(imageContent)>

<cfoutput>
    <img src="data:image/jpeg;base64, #imageContentAsBase64#" />
</cfoutput>
Alex
  • 7,743
  • 1
  • 18
  • 38
0

You can try creating a cfm page that outputs your content using cfcontent as in:

<cfcontent type="image/jpg" file="path/#fielpath#">

Then you would you include THAT cfm page as the source for your image as in

<img src="myFancyImageOuputer.cfm?image=#filepath#">

This should work but it may require some trial and error. :)

Ray has some additional tips here:

http://www.raymondcamden.com/2007/09/14/Serving-up-CFIMages-via-Image-Tags-and-a-NonCF-Friday-contest

Mark A Kruger
  • 7,183
  • 20
  • 21
  • (Edit) True, but I am wondering if they really need cfimage as ultimately it just generates an html `` tag. @MrJdac - What happens if you use `` instead? Worked fine for me in CF11. – Leigh Dec 16 '15 at 18:39
  • Leigh - Alex's answer is maybe what you had in mind here eh? – Mark A Kruger Dec 16 '15 at 18:57
  • Well, I considered base64 but .. was not sure how big their images were. I am just wondering if a basic tag might do the trick here, since it is all server side (and seems to work without specifying mime type). – Leigh Dec 16 '15 at 19:11
  • yeah - but for PDF generation it's probably virtually the same thing - CFDOCUMENT has to grab the image and base64 it anyway to embed it into the doc. – Mark A Kruger Dec 16 '15 at 19:12
  • This solution is actually very similar to how the site serves up attachments in general. The problem seems to be in trying to make cfdocument interpret that this jpg file, without .jpg in the filename, is a for-reals image file. For the moment, I've instituted a horrendous workaround that involves copying the file to a new file with .jpg, merging that image, then deleting it. –  Dec 16 '15 at 20:06
  • MrJdac - so neither my solution or Alex seems to work? – Mark A Kruger Dec 16 '15 at 20:34
  • @MarkAKruger - Honestly not sure how it handles embedding resources on the back end, but if there is a simpler option like , I would just let CF do the heavy lifting. @MrJdac - Did ever you try using ``? Reason for asking is it worked perfectly under CF11. – Leigh Dec 16 '15 at 20:40
  • I don't know why, but is refusing to work for me, I end up with a red X in the PDF. I'm using CF11 too. –  Dec 16 '15 at 21:09
  • @MrJdac - Oh wait.. I was assuming the image path was a URL. If it is a local path, like "c:\path\B71637CB-A49C-0653-EF813918736BDEB7" - yes, that would not work. You would just get the red "X". – Leigh Dec 16 '15 at 22:41
  • You can use a local path - you have to prefix it with file ... as in img="file:///C:\blah\blah.gif" .. – Mark A Kruger Dec 17 '15 at 18:53