This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Outputs raw code instead of pdf

DrBB wrote on 2016-08-03:
Recently overhauled our site and moved to a new server. I'm trying to get an old script to run that used pdfcrowd to generate a pdf. Seems like I have all the pieces in place now, but when I run it from the url I get what looks like the browser's attempt to display a bunch of raw code instead of an actual PDF. It has been a long time since I wrote this script but it all definitely worked on the old site as a of just a month ago, so I'm hoping it's something obvious and I'm just not seeing it. Here's the link I'm using to test it, including values that populate the page that PDFCrowd then converts:

http://www.neaq.org/anispo/pdf_generator.php?donor=Mr.%20fname2%20lname2&animal_name=blacknose%20shark&date=10/31/2014

Here's the pdf generator script:

<?php
$animal_name=$_GET['animal_name'];
$donor=$_GET['donor'];
$date=$_GET['date'];


require 'pdfcrowd.php';


$url="http://www.neaq.org/anispo/pdf_template.php?donor=".$donor."&animal_name=".$animal_name."&date=".$date;
try
{
// create an API client instance
$client = new Pdfcrowd("NEAqMembership", "[my API key]");
$client->setPageWidth('8.5in');
$client->setPageHeight('11in');
// $client->usePrintMedia(true);
$client->setPageMargins("0in", "0in", "0in", "0in");
// $client->setInitialPdfZoomType(Pdfcrowd::FIT_WIDTH);

$pdf = $client->convertURI($url);

// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"animal_sponsorship.pdf\"");

// send the generated PDF
echo $pdf;
}
catch(PdfcrowdException $why)
{
echo "Pdfcrowd Error: " . $why;
}
?>
support wrote on 2016-08-04:
Hello,

I tested the URL and it returns the document as "text/html", but the "Content-Type" header should be set to "application/pdf". That is why you see the raw PDF data. You need to fix this on your side.
DrBB wrote on 2016-08-08:
That makes sense in terms of how it's behaving but I do have it set to application/pdf in the // set HTTP response headers block, as you can see in the script I pasted in above. Is there somewhere else this needs to be set?
DrBB wrote on 2016-08-08:
Saw this in the error log: "Cannot modify header information - headers already sent" and remembered an old 'gotcha' about sending http headers from inside a script. Yup, somehow got some whitespace or gremlin character ahead of the <?php opener. Removed it and it works now--thanks for pointing me in the right direction.