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

Corrupt file - Encoding issue?

bambam wrote on 2012-09-05:

All seems to be working apart from the encoding. the code i'm using is an exact copy of the stuff at

apart from changing the last line in the headers section to

header("Content-Disposition: inline; filename=\"created.pdf\"");

to send the page to the browser instead of download the file - I have tried it in the original form, but get the error trying to open the downloaded files (opening error)

in its current form it just spits out gibberish (pdferror)

Any help or direction would be greatly appreciated



edit: tried a few different things and at one point got a 503 error, but that was just me having too many windows open! still gibberish......
support wrote on 2012-09-05:

the response from your URL contains "Content-Type:text/html; charset=utf-8" which makes the browser interpret it as html instead of pdf.

It seems that Joomla ignores setting the Content-Type header field through the header() function. According to this StackOverflow post the following should work:
$doc =& JFactory::getDocument();
bambam wrote on 2012-09-06:
Hi there,

Thanks for such a quick response, and sorry for being a noob about all this..

I can see that what you posted is the solution, but cant make it work, i either still get the non decoded output (when the headers are not set right, or the browser sets itself to pdf but fails to load the file?

am trying as many variations as i can think of, and will post if i find the solution.

support wrote on 2012-09-06:

your code mixes the generated PDF with some HTML code into a single data stream and sends it to the browser - that's why the browser fails to load it since it is not a valid PDF file.

Please, post the part of your PHP code which calls the API and we will look into it.
bambam wrote on 2012-09-07:
Thank you,

here is the code as it stands at the moment


/*bring needed data from form
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$body1 = $_POST['body1'];
$body2 = $_POST['body2'];

/*create html page
echo $body1;
echo $body2;

/*set doc as pdf
$doc =& JFactory::getDocument();

include 'pdfcrowd.php';

function generatePDF()
    if (!$_GET["pdf"])
        return False;
    try {
        // build the url and remove the pdf field from the query string
        $url = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"];
        if (count($_GET) > 1) {
            $url = $url . "?" . http_build_query($_GET, '', '&');

        // call the API
        $client = new Pdfcrowd("user", "apikey");
        $pdf = $client->convertURI($url);

        // send the generated pdf to the browser
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: inline; filename=\"created.pdf\"");
        echo $pdf;
    catch(PdfcrowdException $why) {
        echo "PDF creation failed: ".$why."\n";

    return True;

    // create an API client instance
    $client = new Pdfcrowd("***", "***");

    if (generatePDF())
catch(PdfcrowdException $why)
    echo "Pdfcrowd Error: " . $why;

support wrote on 2012-09-10:
Currently the view for 'en/report-pdf?pdf=1' sends the following to the browser within a single response:
4043 bytes of html code
140393 bytes of pdf received from the api
53 bytes of html code

The result is a mixup of html and pdf which is not a valid PDF file. You can verify that yourself by saving the file and opening it in a text editor.

You need to get rid of those extra 4096 bytes of html. I can't advise you in this regard as I'm not familiar with Joomla. In general, you should disable any templating, variable expansion, or any other additional processing in the 'en/report-pdf?pdf=1' view.
bambam wrote on 2012-09-10:
Hi there,

I understand the answer, but unfortunately that wont work, the template was custom written, (not by me) and it is bad enough stripping away menus etc, I have changed the way this part will work and had more success.

This thread can be closed as far as i am concerned

Cheers for the great support though.