tags:

views:

1274

answers:

6

I want to open the PDF in browser so that i have wriiten the below code but i have occured the error: "file does not begin with %PDF".

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>

    <div style="width:358px;height:206px;">
     <img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_home_image_117713.jpg"  border="2" width="358" height="206" />
     <img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_mug_image_117713.jpg" border="1" width="68" height="68" style="position:absolute; top:190px; left:25px; z-index:2"/>
     <font style="position:absolute; top:15px; left:15px; z-index:2; border:1px; color:FFFFFF;font-family:impact;font-size:14px;">

     </font>
    </div>
    <div style="width:425px;height:206px;padding-top:5px; padding-left:75px; text-align:center; border:1px;font-family:Arial;font-size:9px;">

    </div>
</body>
</html>

<cfheader name="Content-Disposition" value="inline; filename=Test.pdf">
<cfcontent type="application/pdf">
+3  A: 
<cfheader name="Content-Disposition" value="inline; filename=Test.pdf">
<cfcontent type="application/pdf">

Don't know coldfusion, but that really looks like you're sending HTML to the browser and pretending its PDF. That won't work.

derobert
That's correct. ColdFusion has special tag for converting HTML to PDF to make it working.
Sergii
A: 

I agree with the answer by derobert. The error message you get means that the browser checks if the file is a valid pdf file by looking for the characters %PDF (hex: 25 50 44 46) at the beginning of the file it received. Since you are not sending a pdf file, the signature is not there, hence the error message.

Treb
+1  A: 

From some of my code:

<!--- Force a download, don't cache --->
<cfheader name="Content-disposition" value="inline; filename=#arguments.name#" />
<cfheader name="Cache-control" value="max-age=10" /><!--- No cache doesn't work with IE6 due to bug --->
<cfcontent file="#my.safe_path#" type="#arguments.mime#" />

It must be the ONLY thing output. The difference here is the file attribute being used with cfcontent

SpliFF
+1  A: 

Think you should try following way:

<cfsavecontent variable="htmlContent">
here goes your HTML
</cfsavecontent>

<cfheader name="Content-Disposition" value="inline; filename=Test.pdf">

<cfdocument format="PDF"><cfoutput>#htmlContent#</cfoutput></cfdocument>

P.S. Haven't really tested the code, just a quick example.

Sergii
Sure, you can avoid passing the content via variable, but it is common way in real-life applications.
Sergii
+6  A: 
<!--- TAKES YOUR HTML AND SAVES IT TO A LOCAL VARIABLE --->
<cfsavecontent variable="PDFhtml">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
</head>
<body>
  <div style="width:358px;height:206px;">
    <img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_home_image_117713.jpg"  border="2" width="358" height="206" />
    <img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_mug_image_117713.jpg" border="1" width="68" height="68" style="position:absolute; top:190px; left:25px; z-index:2"/>
    <font style="position:absolute; top:15px; left:15px; z-index:2; border:1px; color:FFFFFF;font-family:impact;font-size:14px;">
    </font>
  </div>
  <div style="width:425px;height:206px;padding-top:5px; padding-left:75px; text-align:center; border:1px;font-family:Arial;font-size:9px;">
  </div>
</body>
</html>
</cfsavecontent>

<!--- USES THE VARIABLE DEFINED ABOVE TO CREATE THE PDF USING CF TAGS --->
<cfheader name="Content-Disposition" value="inline; filename=Test.pdf">
<cfdocument format="pdf">
  <cfoutput>
    #variables.PDFhtml#
  </cfoutput>
</cfdocument>

Just a little side not, you can't make the PDF display in browser from Coldfusion; you’re just generating the PDF with Colfusion. Where the PDF opens up is a option of your default reader, most commonly Adobe Reader. If the PDF isn’t opening up in the browser, you might have “Display PDF in browser” turned off. To Fix that open Adobe Reader and go to Edit->Preference, under "Categories:" click “Internet”. On the right hand side you’ll see a check box, for the “Display PDF in browser” label, make sure it’s clicked.

Stefano DiFabio
A: 

Wrap the html in a cfdocument tag like so

<cfdocument format="pdf">html content</cfdoument>

That should do the trick