1

Web-Service that streams the PDF file:

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: public');
header("Content-length: " . $filesize); 
header('Content-Description: File Transfer');
header('Content-type: application/pdf');
header("Content-Disposition: attachment; filename={$name}");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: max-age=0');  
ob_start();
fpassthru($fp);
$response = ob_get_contents();
ob_end_clean();
return $response;

jQuery code where the request is initiated and response is utilized:

$.ajax({
      url: "/xxx/xxx/1/xxx/5",
      type: "GET",
      async:   true,
      dataType: 'binary',
      headers:{'Content-Type':'application/pdf',
                'X-Requested-With':'XMLHttpRequest',
      },
      processData: false,
      success: function(data,status,xhr) {
        var file = new Blob([data], {type: 'application/pdf'});
        var fileURL = URL.createObjectURL(file);
        var a = document.createElement("a");
        a.href = fileURL;
        a.download = "Testing.pdf";
        document.body.appendChild(a);
        a.click();
        $(window).on('focus', function(e) {
          $('a').remove();
        });
      },
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
           console.log("fail");
      }  

The PDF file is generated perfectly, and opens automatically. The only problem here is the PDF content comes empty when should have text!

Does anybody have an idea why this is happening?

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Bruno
  • 131
  • 3
  • 17
  • The only thing I can think of is that the PDF you're sending doesn't have the text you're saying it should. If it just wasn't adding it, I would expect to see a corrupted PDF, so the fact it's working aside from this text implies it's not there to begin with. – Jonnix Oct 26 '15 at 16:15
  • There is text there, for sure! – Bruno Oct 26 '15 at 16:17
  • @Bruno `jQuery.ajax()` does not have standard option for `dataType:"binary"` without adjusting `$.ajax()` source , see https://gist.github.com/SaneMethod/7548768 . Try using native `XMLHttpRequest` , see http://stackoverflow.com/questions/12876000/how-to-build-pdf-file-from-binary-string-returned-from-a-web-service-using-javas/ . – guest271314 Oct 26 '15 at 16:18
  • @Bruno How have you confirmed this? PHP doesn't randomly remove bits and pieces from files on its own. Certainly not pieces of text when PHP doesn't really have a context of what a PDF actually is. – Jonnix Oct 26 '15 at 16:19
  • @Jon I checked because I have another WS for testing, and on this one i made the XHR request and i got the pdf perfectly! – Bruno Oct 26 '15 at 16:28
  • I'm not using xhr requests due the WS returns a json object! – Bruno Oct 26 '15 at 16:29
  • @Bruno are you saying one environment works and another doesn't? – Jonnix Oct 26 '15 at 16:30
  • yes, and i don't know why – Bruno Oct 26 '15 at 16:32
  • Seriously, have you checked the PDF on the environment that you're having problems with. – Jonnix Oct 26 '15 at 16:36
  • why do you need ajax for this in the first place? just use `window.location` – charlietfl Oct 26 '15 at 16:39
  • 1
    _"the WS returns a json object!"_ Where is `json` returned from `php` ? Should `return` be replaced with `echo` ? Is response`binary`or`json` ? – guest271314 Oct 26 '15 at 16:40
  • The situation here is, i have a document which is encrypted when uploaded, and when the user requests the WS first i need to decrypt and after send to the user. What is the best way to send the document to the user? i can't use window.location, because in this case i would need to recreate the decrypted file in a temporary folder at least. And i don't want to have the decrypted file in a physic place in the server. – Bruno Oct 27 '15 at 07:50
  • @Bruno did you ever resolve this? guest271314's comments look like good hints, unless the value from `$response` actually does get sent to the browser... – Sᴀᴍ Onᴇᴌᴀ Apr 27 '17 at 21:17

0 Answers0