jQuery print iframe pdf 适用于本地主机,但不适用于服务器
jQuery print iframe pdf works on localhost but not in server
下面的代码在我的本地环境中完美运行。但是,相同的在线代码不起作用。我没有收到任何错误,iFrame 只是没有按预期弹出。
src
属性总是按预期在 iframe 中更改...
if($('#iFramePdf').length > 0)
$('#iFramePdf').attr('src', json.pdf);
else
$('body').append('<iframe class="hidden" id="iFramePdf" src="' + json.pdf + '"></iframe>');
// enough time for the src being changed
setTimeout(function()
{
$('#iFramePdf').get(0).focus();
$('#iFramePdf').get(0).contentWindow.print();
}, 1000);
可能出了什么问题?
编辑1:这似乎可能是由于content-type
标题引起的?json.pdf 具有如下所示的绝对 URL:
http://mywebsite.com/public/files/somefile.pdf
并且它不会打开镶边预览对话框。但是,如果我删除http://mywebsite.com
并仅发送public/files/somefile.pdf
- 就像相对路径一样,Chrome 预览对话框会打开,但它会显示我的 404 页面。
检索 PDF 的函数ajax_get
具有以下响应标头:
Cache-Control:no-cache, no-store, must-revalidate
Connection:keep-alive
Content-Encoding:gzip
Content-Length:166
Content-Type:text/html; charset=UTF-8
Date:Tue, 26 Jan 2016 09:24:42 GMT
Expires:0
Pragma:no-cache
Server:nginx
Vary:User-Agent,Accept-Encoding
X-Powered-By:PHP/5.6.17
并检索以下 json: {"status":1,"msg":"Added successfully", "pdf":"http:'/'/mywebsite.com'/public'/files'/somefile.pdf"}
收到 json 并将 pdf 的路径发送到 chrome 控制台的网络选项卡中的 iFrame 后,pdf 将作为响应标头检索:
Accept-Ranges:bytes
Cache-Control:no-cache, no-store, must-revalidate
Connection:keep-alive
Content-Length:52611
Content-Type:application/pdf
Date:Tue, 26 Jan 2016 09:24:42 GMT
Expires:0
Last-Modified:Tue, 26 Jan 2016 09:24:42 GMT
Pragma:no-cache
Server:nginx
Vary:User-Agent
编辑2:我只是尝试从PHP页面检索PDF没有成功,如下所示:
function display_pdf($url)
{
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="testing.pdf"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo file_get_contents(base_url() . $url);
}
在发送到iFramePdf
之前在javascript中:
json.pdf = json.pdf.replace('http://mywebsite.com/', '');
json.pdf = base_url + 'backend/files/display_pdf/' + json.pdf;
最终网址如下所示:http://mywebsite/backend/files/display_pdf/public/files/somefile.pdf
如果我在浏览器中输入该链接,它可以工作..但在iframe中,它仍然无法从Chrome打开打印预览。
编辑3:我又运行了一些测试,问题在iframe上仍然存在,因为它以其他方式工作。例如,下面的代码打开一个新选项卡并自动打开 chrome 的打印预览对话框,没有任何问题。
var win=window.open('about:blank');
win.document.write('<html><head></head><body>');
win.document.write('<iframe frameBorder="0" align="center" src="' + json.pdf + '" onload="test()" style="width: 100%; height: 100%"></iframe>');
win.document.write('<scr'+'ipt>function test(){window.focus();window.contentWindow.print()}</sc'+'ript></body></html>');
win.document.close();
if (window.focus) {win.focus()}
但我不想打开一个新标签。所以我尝试在我所在的选项卡中打开一个选项卡,如下所示:
var printWindow = window.open(json.pathToPdf, "printWindow", "scrollbars=yes");
printWindow.focus();
printWindow.print();
并且可以工作,它会在我所在的选项卡中打开一个小选项卡,但是它不会聚焦 chrome 打印预览对话框,如果我在全屏模式下工作,它会将其关闭。
编辑4:问题实际上是因为PDF。如果我将代码从:
$('body').append('<iframe class="hidden" id="iFramePdf" src="' + json.pdf + '"></iframe>');
自
$('body').append('<iframe class="hidden" id="iFramePdf" src="http://mywebsite.com/public/images/someimage.png"></iframe>');
或
$('body').append('<iframe class="hidden" id="iFramePdf" src="http://mywebsite.com/somepage.php"></iframe>');
它可以毫无问题地工作。
如果我将PDF直接设置为src
打印预览对话框不会打开,但PDF会加载到iframe中。
$('body').append('<iframe class="hidden" id="iFramePdf" src="http://mywebsite.com/public/files/somepdf.pdf"></iframe>');
经过我所有的尝试,这似乎真的是一个没有人知道的错误。它适用于本地主机,但不适用于服务器。唯一的问题是PDF文件,因为图像和页面工作正常。
我最终可能会从生成的 PDF 文件创建图像,这不是一个好的解决方案,因为我可以有多个 PDF 页面。
无论如何,如果有人知道任何解决方案或希望我尝试任何事情,我很乐意这样做。
单击表格中的打印图标时打印pdf的工作代码。
1)调用js函数的图标代码:
<a href="javascript:printReport('<?= $userReport->pdf_file_name ?>')"><i class="btn-icon-only icon-large icon-print" title="Print Report"> </i></a>
2)一个div元素,用作占位符来显示和打印pdf:
<div id="view-report-holder"></div>
3) JS功能打印PDF报告:
function printReport(pdfFileName)
{
$('#view-report-holder').hide();
$('#view-report-holder').html('');
$('<iframe>', {
src: '<?= $uploadDir['baseurl']."/pdf_reports/" ?>' + pdfFileName,
frameborder: 0,
id: 'view-report',
scrolling: 'no'
}).appendTo('#view-report-holder');
var printTag = document.getElementById('view-report');
printTag.contentWindow.print();
return false;
}
在表格下显示pdf的链接和功能:(打印功能不需要,顺便说一句)
<a href="javascript:viewReport('<?= $userReport->pdf_file_name ?>')"><i class="btn-icon-only icon-large icon-eye-open" title="View Report"> </i></a>
...
function viewReport(pdfFileName)
{
$('#view-report-holder').show();
$('#view-report-holder').html('');
$('<iframe>', {
src: '<?= $uploadDir['baseurl']."/pdf_reports/" ?>' + pdfFileName,
frameborder: 0,
scrolling: 'no'
}).appendTo('#view-report-holder');
$('html, body').animate({
scrollTop: $("#view-report-holder").offset().top
});
return false;
}
- HTML5 Canvas toDataURL(“image/png”)适用于一些绘画,而有些则不然,不确定原因
- JavaScript不显示剩余字符,但适用于其他页面
- 我如何完善这个Javascript代码,使其只适用于来自图像的链接(而不是来自文本的链接)
- 为什么这只适用于身体部分而不是头部
- 为什么不't此正则表达式适用于重复出现的对
- document.getElementById 对内部 html 的调用适用于 $_POST 和 $_GET 变量,但不
- JavaScript 位置适用于本地主机而不是服务器
- Rally App SDK 2.0:Rallymultiobjectpicker,适用于不显示displayField的
- jQuery事件在Mozilla上不起作用,并且适用于其他浏览器
- Angular.js - 控制器不为索引执行,适用于其余模板
- D3 .on 更改适用于文本而不是图表
- Zillow 数据 - json_encode不起作用 - 适用于常规变量
- 弹出模式仅适用于表格中的第一个元素,第二次单击后不会再次打开
- Phonegap Android项目适用于模拟器,而不是实际的手机
- ajax jonp 不应该适用于 请求的上不存在“访问控制-允许源”标头
- AngularJS:ng-单击不起作用,适用于<按钮>
- Javascript不会在移动设备上加载.适用于桌面
- 为什么我的代码适用于高度:100px,而不是高度:100%
- 拖放区.js + Laravel-多文件上传不起作用(仅适用于一个文件)
- 哪些限制适用于不透明响应