Display PDF from BLOB file

Alex Moran
3 min readJul 3, 2020
Photo by Caspar Camille Rubin on Unsplash

So I spent some time searching for answers and came across parts of answers that worked and didn’t work which is why I am writing this quick guide.

In this example I am using the following set up. Codeigniter, PHP, Javascript, Bootstrap and MYSQL.

MYSQL

When storing the PDF document in MYSQL I am using mediumblob as some of the pdfs are larger. For the preparing of the file I use Ajax to upload the file and then within the controller of codeigniter checking that the file is actually there.

The final step is addslashes this escapes characters that need it for storing in the database.

if ($_FILES['file']['size'] > 0) {
$fileName = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];

$fp = fopen($tmpName, 'r');
$contents = fread($fp, filesize($tmpName));
$contents = addslashes($contents);
fclose($fp);

}

Once this part is done then just use your existing method to insert into MYSQL.

PHP

For the viewing of the PDF I have a table showing the pdfs with a button.

VIEW PDF Button

The View pdf button links to this javascript function.

function view_pdf(id){
BootstrapDialog.show({

title: "View PDF",
message: $('<div></div>').load("<?php echo base_url("pdfview/display_pdf"); ?>/"+id),
cssClass: 'wide-dialog',
buttons: [{
label: 'Cancel',
cssClass: 'btn-danger',
action: function(dialog) {
dialog.close();
}
}
, {
label: 'Ok',
cssClass: 'btn-primary',
action: function(dialog) {

dialog.close();

}
}
]
});
}

The button does this it loads a popup view which loads the pdf within the view.

The button calls the function below. From this function we get the ID and retrieve the contents from MYSQL.

They key part for me was remembering to stripslashes if you addslashes earlier. Setting the headers is next.

public function display_pdf($id){

$text = $this->pdf_model->get_pdf($id);


$content = stripslashes($text["file"]);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename=name.pdf');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
$this->load->view("administrator/load_pdf", [
"title"=>"Display PDF",
"pdf"=>$content,
]);

}

The view that gets called contains this php file called load_pdf

<object data="data:application/pdf;base64,<?php echo base64_encode($pdf);?>" type="application/pdf" height="100%" width="100%"></object>

Some caveats and notes.

Your miles may vary with the huge amount of variance with pdfs and versions. For the pdfs I have this has worked for all of them. The actual functions should be pretty straightforward for including in whatever system you use.

Thank you for reading. If you enjoy or find this helpful you could look at joining Medium. Its €5 a month you can sign up using this link or if you sign up using my link I will get a small commission from it which is https://medium.com/@alexrmoran/membership

--

--

Alex Moran

Full-Stack Developer looking for the next big thing. If you want to support me please sign up using my link below https://medium.com/@alexrmoran/membership