我有一个自动生成的PDF文件,我需要在HTML中显示该PDF文件。我的问题是:如何在HTML中使用PDF .js显示本地PDF文件?PDF文件是否应该按照某些标准生成?
当前回答
在html页面的pc很容易实现
<embed src="study/sample.pdf" type="application/pdf" height="300px" width="100%">
但PDF显示在移动的这段代码是不可能的,你必须需要一个插件
如果你没有响应你的网站。然后上面的代码pdf不显示在手机上,但你可以把下载选项后的代码
<embed src="study/sample.pdf" type="application/pdf" height="300px" width="100%" class="responsive">
<a href="study/sample.pdf">download</a>
其他回答
1. 浏览器原生HTML内联嵌入:
<embed
src="http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&navpanes=0&scrollbar=0"
type="application/pdf"
frameBorder="0"
scrolling="auto"
height="100%"
width="100%"
></embed>
<iframe
src="http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&navpanes=0&scrollbar=0"
frameBorder="0"
scrolling="auto"
height="100%"
width="100%"
></iframe>
Pro:
没有PDF文件大小限制(甚至数百MB) 这是最快的解决方法
缺点:
它不能在移动浏览器上运行
2. 谷歌文档查看器:
<iframe
src="https://drive.google.com/viewerng/viewer?embedded=true&url=http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&scrollbar=0"
frameBorder="0"
scrolling="auto"
height="100%"
width="100%"
></iframe>
Pro:
适用于桌面和移动浏览器
缺点:
25MB文件限制 需要额外的时间下载查看器
3.嵌入PDF的其他解决方案:
https://mozilla.github.io/pdf.js https://pdfobject.com https://viewerjs.org
重要提示:
请检查X-Frame-Options HTTP响应报头。它应该是SAMEORIGIN。
X-Frame-Options SAMEORIGIN;
我知道你想使用HTMl显示,但你也可以通过指出路径使用php打开PDF文件,浏览器将在几个简单的步骤中呈现它
<?php
$your_file_name = "url_here";
//Content type and this case its a PDF
header("Content-type: application/pdf");
header("Content-Length: " . filesize($your_file_name ));
//Display the file
readfile($your_file_name );
?>
我使用谷歌Docs嵌入式PDF查看器。这些文档不必上传到谷歌docs,但必须在网上可用。
<iframe src="https://docs.google.com/gview?url=https://path.com/to/your/pdf.pdf&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>
这个方法也很有用:
在桌面和移动浏览器上显示pdf文件:
<object data="./filename.pdf" type="application/pdf" width="500px" height="600px"> <p>你的浏览器没有PDF插件。 <a href="./filename.pdf">点击这里下载PDF文件</a></p> . PDF < /对象>
在html页面的pc很容易实现
<embed src="study/sample.pdf" type="application/pdf" height="300px" width="100%">
但PDF显示在移动的这段代码是不可能的,你必须需要一个插件
如果你没有响应你的网站。然后上面的代码pdf不显示在手机上,但你可以把下载选项后的代码
<embed src="study/sample.pdf" type="application/pdf" height="300px" width="100%" class="responsive">
<a href="study/sample.pdf">download</a>
推荐文章
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?
- 输入触发器按钮单击
- Angular 2模板中的标签是什么意思?
- 如何设置身体高度溢出滚动
- 在输入type="number"时禁用webkit的旋转按钮?
- 如何在另一个元素之后添加一个元素?
- 我如何有效地解析HTML与Java?
- “ ”和“”有什么区别?