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

Cannot open PDF file

iamthida wrote on 2011-03-17:
Hi guys,

I'm trying to create pdf file from URI using PHP. Here is my code,

require_once("include/pdfcrowd.php");
try
{
	 $client = new Pdfcrowd("my_username", "my_api_key");
	 header('Content-type: application/pdf');
	 header('Content-Disposition: attachment; filename="google.pdf"');
	 $client->convertURI('http://www.google.com', fopen('google.pdf', 'wb'));
	 echo "remaining tokens: " . $client->numTokens();
}
catch(PdfcrowdException $e)
{
	 echo $e->getMessage();
}


Pdf file I've got is always 1KB and cannot open with adobe reader correctly. The error shows by adobe reader when I tried to open is "Adobe Reader could not open 'google.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."

Please let me know which part I missed? or give me some suggestion please.

(Note: without the 2 header() lines, the file has not been forced to download, so I put it as you mentioned in https://pdfcrowd.com/forum/archive/read/?3,143)

By the way, thanks for your hard working to give us a nice and easy tool like this! :)
support wrote on 2011-03-17:
Try this:
$client = new Pdfcrowd("***", "***");
$pdf = $client->convertURI("http://www.google.com");

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

echo $pdf;


The problem with your code is that this line

echo "remaining tokens: " . $client->numTokens();


outputs the number of the remaining tokens instead of the generated PDF. The browser assumes that it is a PDF and saves it to a file. Of course, a PDF viewer can't read it since the file contains only a single number.
iamthida wrote on 2011-03-17:
Thanks a million! It works now, hoorayyyyyyyyyyyyyyy ^^