Skip to main content
Known Participant
May 23, 2016
Answered

Using cfcontent to pull pdf's from file

  • May 23, 2016
  • 1 reply
  • 2686 views

Hello,

I have searched the forums extensively and still have not found a solution. What I am trying to achieve is simple, but am having problems getting it to work.

I want to pass a dynamic url string from a web mapping application to a cold fusion page that then opens the pdf file on the users browser. This is an example of the url that will be passed to this page, and based on what feature the user selects, subdir and page_id will be dynamic:

http://localhost/displayImage.cfm?subdir=60&page_id=3807444

Here is what I have so far for displayImage.cfm,it is just a start, I know I need more, possibly cfheader content? When I run this code I get an error saying the file cannot be found.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

  <title>Untitled</title>

</head>

<body>

<cfquery name="Surveys" datasource="sire" dbtype="odbc">

    select *

  from slco_sire.dbo.surveys_doc INNER JOIN slco_sire.dbo.surveys_page

  ON slco_sire.dbo.surveys_doc.doc_id = slco_sire.dbo.surveys_page.doc_id

</cfquery>

<cfset picture="#url.page_id#">

<cfset sub="#url.subdir#">

<cfcontent file="\\path\to\file\#sub#\#picture#" type="application/pdf" reset="yes">

</body>

</html>

Any help would be greatly appreciate, I am stuck.

This topic has been closed for replies.
Correct answer EddieLotter

The example pdf shows up! Maybe adding the variables of the page_id and subdir and what is causing problems with the original code?


No, the variables are not the problem.

The ColdFusion service runs under a specific user account and it is that account that must have security privileges to access the folder on the network where the PDF files reside.

You will need to have your network administrator check that for you.

Cheers

Eddie

1 reply

EddieLotter
Inspiring
May 23, 2016

Use the following to display useful debugging information so you can see what is happening:

<!--- <cfcontent file="\\path\to\file\#sub#\#picture#" type="application/pdf" reset="yes"> --->

<cfif fileExists("\\path\to\file\" & sub & "\" & picture)>

The file exists.

<cfelse>

File not found: <cfoutput>\\path\to\file\#sub#\#picture#</cfoutput>

</cfif>

Notice that I commented out the cfcontent tag, otherwise you won't see the result of this change. Also, the reset parameter is ignored when serving a file.

Cheers

Eddie

Known Participant
May 23, 2016

Hi Eddie,

Thank you so much for your help!

When I add that code, the "File not found" error appears in the browser. If I hard code the path to the pdf in the browser it finds it, but I have to add the .pdf for this to work.

This works:

file://path/to/file/60/3831529.pdf

This does not

file://path/to/file/60/3831529

Do I need to do that in the code? I thought the browser would know what to do with the application/pdf info in the cfcontent tag.

EddieLotter
Inspiring
May 23, 2016

Yes, you need to add the extension. The cfcontent tag makes no assumptions about the file name that you pass to it. It must be a fully qualified path and file name of an existing file, otherwise it will fail.

Cheers

Eddie