0

This is my jQuery: $( document ).ready(function() { var instrID; var cat;

    $(window).load(function(){

    });

            $.post('ajax.php', {InstrumentID: instrID, catView: "pdf"}, function(data){

            $('#displayPDF').append("<php> header('Content-type: application/pdf') </php>");
            $('#displayPDF').append("<php> echo("+ data +") </php>");
        });

This is my ajax or ajax.php:

<?php

include '../include/xxxxx.php';

$instrumentID = $_POST['InstrumentID'];
$category = $_POST['catView'];


$sql = "SELECT * FROM `xxxxx` WHERE `InstrumentID` = '" . $_POST['InstrumentID'] . "'";


$results = mysql_query($sql);

    if($category == "pdf")
{
    header("Content-type: application/pdf");
    echo (mysql_result($results, 0, 'Instrument'));
}
?>

This is my div displayPDF It's empty:

<div id="displayPDF">

</div>

The jQuery and the div are in the same php file. I am wanting to display a pdf in the same page that the click event happens. Everything is working except for getting the pdf. When the pdf gets echoed to the div it just comes back as a bunch of characters. The pdf I am trying to display is less than 1 mb. Any ideas would be greatly appreciated.

3 Answers 3

3

Maybe this is not an answer to the specific question but, This question comes up at very first google search, so I would like to share my approach for downloading pdf when it's blob data.

$.ajax({
  url: 'someurl',
  method: 'get',
  data: { param1: value1, param2: value2 },
  xhr: function() {
    const xhr = new XMLHttpRequest();
    xhr.responseType= 'blob'
    return xhr;
  },
  success: function (blob) {
    const link=document.createElement('a');
    link.href=window.URL.createObjectURL(blob);
    link.download="my-pdf-file-name";
    link.click();
  }
});
Sign up to request clarification or add additional context in comments.

Comments

2

You haven't set a value for instrID, also you're not sanitizing for input.

Anyway instead of using ajax you can just embed the pdf into the page

var source = 'ajax.php?InstrumentID='+encodeUriComponent(instrID)+'&catView=pdf';
$('#displayPDF').append('<object data="'+source+'" type="application/pdf">'+
                        '<embed src="'+source+'" type="application/pdf"/></object>');

and then use $_GET instead of post in your php.

4 Comments

both: $instrumentID = $_POST['InstrumentID']; $category = $_POST['catView']; should be $instrumentID = $_GET['InstrumentID']; $category = $_GET['catView'];
@user908759 yes, the change is from an ajax post request to a get request form an embedded object.
I changed the posts to gets and still no luck. Is there another way to display a pdf blob using php, mysql, and jquery? I've been searching like crazy and I haven't found much help. Thanks you though.
thanks to your help I just figured it out... check my new answer
0

I don't think you can display a PDF inline in this manner. Try switching to an iframe - that should work. That is set the location of the iframe to your ajax.php.

2 Comments

I'm guessing the iframe would go in my div. Do you have an example?
No. I dont use iframes. There should be plenty of examples on google.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.