So, I'm passing some values via jQuery to the server, which generates PDF garble. It goes something like this:
$.post('/admin/printBatch',
data, // Some vars and such
function(data){
if(data) {
var batch = window.open('','Batch Print','width=600,height=600,location=_newtab');
var html = data; // Perhaps some header info here?!
batch.document.open();
开发者_如何学JAVA batch.document.write(html);
batch.document.close();
$( this ).dialog( "close" ); // jQuery UI
} else {
alert("Something went wrong, dawg.");
}
return false;
});
The output file looks roughly like so:
$pdf->AddPage(null, null, 'A PDF Page');
//....
$pdf->Output('', 'I'); // 'I' sends the file inline to the browser (http://fpdf.org/en/doc/output.htm)
What gets rendered to the browser window:
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream ...
I'm missing something major, I just know it... thoughts?
Thanks, guys.
By the looks of things, the PDF is reasonably well formed. As such, I suspect you simply need to set the appropriate content header when you're outputting the PDF via PHP using the header function:
header('Content-type: application/pdf');
N.B.: This must be the first output from the PHP - it won't work if there's preceding HTML, blank space, etc.
In an ideal world, you'd also set the content length, etc. via...
header('Content-Length: '.filesize($pathToYourPDF));
...or...
header('Content-Length: '.strlen($pdfData));
...if you're generating the PDF programmatically.
UPDATE
To clarify, I suspect you'll need to change your window.open to read the above directly from a PHP served URL for the above to work. (Not quite sure why you're not just doing this in the first place, but I guess there's a good reason.)
I would rather set URL
for the popup window, pointing to the php script that outputs PDF.
If you absolutely have to do it this way, data-uri might help:
var batch = window.open(
'data:application/pdf,'+encodeURIComponent(data),
'Batch Print',
'width=600,height=600,location=_newtab'
);
but I have not tested it, and even if it works in normal browsers, in IE it is sure to fail.
I have tested this for my application and my conclusion is that you must use an iframe (where show_pdf returns "$pdf->Output('', 'I');"):
$.ajax({
url: "http://www.somesite.com/documents/generatePDF",
success: function(){
$('#theDocument').html('<iframe id="some_id" class="some_pdf" style="display:none" src="http://www.somesite.com/documents/show_pdf" width="100%" height="450" scrollbar="yes" marginwidth="0" marginheight="0" hspace="0" align="middle" frameborder="0" scrolling="yes" style="width:100%; border:0; height:450px; overflow:auto;"></iframe>');
}
});
$('#dialog-document').dialog('open');
This worked for me: Change
$pdf->Output('', 'I');
for
$return = $pdf->Output('name.pdf', 'S');
$return = base64_encode();
$return = 'data:application/pdf;base64,'.$return;
echo json_encode($return);
now, you should, in your javascript file, get the return and open a new window
success: function( data ) {
var pdf = JSON.parse(data);
window.open(pdf);
}
It will open a new tab on your browser with the pdf.
精彩评论