我有一个自动生成的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>

其他回答

你可以使用

<iframe src="your_pdf_file_path" height="100%" width="100%" scrolling="auto"></iframe>

或者,如果你想让它占据整页:

<a href="your_pdf_file_path">Link</a>

最简单的方法是,

<iframe src="pdf-link">
</iframe>

如果它仍然下载而不是查看,检查服务器响应头,它应该有,Content-Disposition:内联,而不是,Content-Disposition:附件。

我知道你想使用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 );
?>

gofilord提供的答案是正确的,但我也可能会补充说,如果你创建一个空桶,将你的pdf倒入,你应该给它一个src=""像…

<embed id="fooEmbed" src="">

...在HTML和javascript…

document.getElementById('fooEmbed').src = 'bar.pdf';

...它会起作用的。如果你这样做…

<embed id="fooEmbed">

...这是行不通的。

我以前有过类似的东西,通常使用标签

<a href="path_of_your_pdf/your_pdf_file.pdf" tabindex="-1"><strong>click here</strong></a>

但是像上面提到的,找到一些其他的方法是很有趣的!