因此,我通过jQuery将一些值传递给服务器,服务器将生成PDF乱码。它是这样的:
$.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();
batch.document.write(html);
batch.document.close();
$( this ).dialog( "close" ); // jQuery UI
} else {
alert("Something went wrong, dawg.");
}
return false;
});输出文件大致如下所示:
$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)渲染到浏览器窗口的内容:
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream ...我错过了一些重要的东西,我就是知道...有什么想法?
谢谢你们。
发布于 2011-01-07 05:46:34
从外观上看,PDF格式相当良好。因此,当您使用header函数通过PHP输出PDF时,我怀疑您只需要设置适当的内容标题:
header('Content-type: application/pdf');注意:这必须是PHP的第一个输出-如果前面有HTML、空格等,它就不会工作。
在理想情况下,您还可以通过以下方式设置内容长度等:
header('Content-Length: '.filesize($pathToYourPDF));...or...
header('Content-Length: '.strlen($pdfData));你正在以编程的方式生成...if。
更新
为了澄清,我怀疑您需要更改您的window.open,以便直接从提供服务的URL中读取上面的内容,才能使上面的内容生效。(我不太确定为什么你一开始就不这么做,但我想这是有原因的。)
发布于 2011-01-07 05:55:49
我已经对我的应用程序进行了测试,我的结论是您必须使用iframe (其中show_pdf返回"$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');发布于 2011-01-07 06:00:32
我宁愿为弹出窗口设置URL,指向输出PDF的php脚本。
如果您一定要这样做,data-uri可能会有所帮助:
var batch = window.open(
'data:application/pdf,'+encodeURIComponent(data),
'Batch Print',
'width=600,height=600,location=_newtab'
);但我还没有测试过它,即使它在普通浏览器中工作,在IE中也肯定会失败。
https://stackoverflow.com/questions/4620195
复制相似问题