How to Retrieve a PDF file from the Database and Display It on a Joomla website

One of the requirements on a project that we’re currently working on was to display PDF files on a Joomla website. This might seem very easy – except that the PDF document is stored in the database (instead of the filesystem) and must be displayed on a separate window and not inline.

The process was simple, all we needed to do was to create a function called showPDF($id) that will retrieve the content of the file (based on its id) from the database, modify the headers, and that’s it. So here’s the code of the function that we tried:

function showPDF($id){
    $query = "SELECT pdf_content FROM #__pdf_files WHERE id=" . $id;
    $db->setQuery($query);
    $pdfContent = $db->loadResult();
    header('Content-type: application/pdf');
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");
    header("Content-Disposition: inline;filename='[name of the PDF file]'");
    header("Content-length: ".strlen($pdfContent));
    echo($pdfContent);
}

Unfortunately, when we called the function from our (custom made) component, it was displaying gibberish. Here’s what we were seeing (we have removed the template because we would like our client to remain anonymous):

Gibberish

Figure 1: Gibberish is being displayed instead of the actual PDF file (Notice how the content starts with ‘%PDF-1.4’)

So we thought “Hmmm… Let’s try using Joomla’s built in function to set the content-type” and so we added the following lines at the top of the function:

	$doc =& JFactory::getDocument();
	$doc->setMimeEncoding('application/pdf');

We thought that we will see the PDF successfully displayed when adding the above lines, but instead the following message popped up: “File does not begin with ‘%PDF-‘.”

File does not begin with %PDF-

Figure 2: Adobe Acrobat error message (Note: In case you’re wondering, we got a snapshot of only the selected popup using ALT + PrintScreen – try it!)

When we saw that the page was also displaying the template, we said to ourselves: “What were we thinking?” Since we’re trying to display the file from within Joomla, then Joomla will, by default, use its template engine to generate the page (which will break the content of the PDF file). Since we wanted the PDF generation process to be part of our Joomla component (we didn’t want to create an independent file called download.php that will be used to display the PDF), we had to get rid of all the content generated by Joomla and only display our content. Here’s when the die() function came to the rescue:

function showPDF($id){
    $query = "SELECT pdf_content FROM #__pdf_files WHERE id=" . $id;
    $db->setQuery($query);
    $pdfContent = $db->loadResult();
    header('Content-type: application/pdf');
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");
    header("Content-Disposition: inline;filename='[name of the PDF file]'");
    header("Content-length: ".strlen($pdfContent));
    die($pdfContent);
}

Instead of echoing the content, we issued a die function which will display the content of the PDF but will stop at that! die() is a PHP function that will instruct the PHP engine to stop processing any further instructions on the page.

As you can see, displaying the content of a PDF file from a database on the fly is not that complicated after all! All we needed to do was to follow the same standard process and use die() instead of echo().

If you need help integrating PDF functionality on your website, then feel free to contact us. We’re always ready to help and our prices are very affordable!

No comments yet.

Leave a comment