Display PDF from BLOB file

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.


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);


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


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


The View pdf button links to this javascript function.

function view_pdf(id){

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) {
, {
label: 'Ok',
cssClass: 'btn-primary',
action: function(dialog) {



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",


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



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Moran

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