把pdf文件放在html页面中显示的方法

html-css07

把pdf文件放在html页面中显示的方法,第1张

方法一

方法二

以上两种方法都去掉了body的margin,禁用了滚动条

参考 https://blog.csdn.net/lanxe/article/details/7705649

把PDF放在自己制作的html静态页面上,首先保证客户端安装有可以直接打开PDF的软件,比如:Adobe Reader

具体解决办法有三种,针对不同的浏览器兼容性方面要做稍微调整。

一、使用 <OBJECT>标记在HTML 中嵌入PDF 文档

如果您的用户使用与 Internet Explorer 兼容、支持 ActiveX 控件的浏览器,您可用 <OBJECT>标记嵌入 PDF 文档,而不需用 <EMBED>标记。与Internet Explorer 3.0 或更高版本兼容的浏览器能支持 <OBJECT>标记。

具体代码如下:

如无法查看,请先下载安装Adobe Reader X

<p style="margin-top: -2pxmargin-bottom: 0">

<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" id="Pdf1" width="730" height="606">

<param name="_Version" value="327680">

<param name="_ExtentX" value="19315">

<param name="_ExtentY" value="16034">

<param name="_StockProps" value="0">

<param name="SRC" value="pdf路径">

</object>

直接嵌入PDF可能会显示PDF头部的功能按钮,可以通过设置margin-top: -2px的值予以隐藏。相关参数调整嵌入的PDF的高宽。

二、使用iframe嵌入pdf

使用iframe包含pdf文件,格式上面就没法保证,显示出来的样式不太好看。 具体代码如下:

<iframe src="pdf路径" with="" height="" scroll="no"></iframe>

三、直接打开pdf

直接打开pdf,使用连接形式打开,如果客户端有安装PDF阅读软件,可以直接打开,但是会整个显示,全屏都是PDF内容了。

具体代码如下:

<a href="pdf路径">XXpdf</a>

iframe嵌入文件是最简单的,直接加载就好.

可以通过使用Spire.Doc for Java进行转换。

首先需要安装Spire.Doc for Java。可在 Java 程序中添加 Spire.Doc for Java 文件作为依赖项。JAR 文件可以从此链接下载。 如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而轻松地在应用程序中导入 JAR 文件。

<repositories>

<repository>

<id>com.e-iceblue</id>

<name>e-iceblue</name>

<url>https://repo.e-iceblue.cn/repository/maven-public/</url>

</repository></repositories><dependencies>

<dependency>

<groupId>e-iceblue</groupId>

<artifactId>spire.doc</artifactId>

<version>5.2.3</version>

</dependency></dependencies>

具体分为以下两种情况:

HTML String另存为PDF格式

Java代码如下:

import com.spire.doc.*import java.io.*public class htmlStringToWord {

public static void main(String[] args) throws Exception {

String inputHtml = "InputHtml.txt"

//新建Document对象

Document document = new Document()

//添加section

Section sec = document.addSection()

String htmlText = readTextFromFile(inputHtml)

//添加段落并写入HTML文本

sec.addParagraph().appendHTML(htmlText)

//文档另存为PDF

document.saveToFile("HTMLstringToPDF.pdf", FileFormat.PDF)

}

public static String readTextFromFile(String fileName) throws IOException{

StringBuffer sb = new StringBuffer()

BufferedReader br = new BufferedReader(new FileReader(fileName))

String content = null

while ((content = br.readLine()) != null) {

sb.append(content)

}

return sb.toString()

}

}

2.HTML file另存为PDF格式

import com.spire.doc.*import com.spire.doc.documents.XHTMLValidationTypepublic class htmlFileToWord {

public static void main(String[] args) throws Exception {

//加载HTML文档

Document document = new Document()

document.loadFromFile("InputHtmlFile.html", FileFormat.Html, XHTMLValidationType.None)

//文档另存为PDF

document.saveToFile("Result.pdf",FileFormat.PDF)

}

}

希望对您有帮助。