DF转HTML在网络应用中越来越受到重视,PDF作为一种电子文件格式广泛应用于各种场景,例如在线教育、电子商务等。但是PDF作为一种专有格式,在网络应用中存在一定的限制,比如不便于搜索引擎索引的问题。因此,将PDF转换为HTML格式就显得尤为重要。HTML作为一种开放标准,在浏览器中的兼容性强,结构清晰,方便搜索引擎抓取,能很好地解决PDF在网络应用中的一些问题。那你们知道PDF怎么转HTML吗?下面就为大家分享几种不错的方法。
以下是可以将PDF转HTML的方法:
一、使用全能PDF转换助手将PDF转HTML
这是一款功能丰富的PDF转换工具,支持将PDF文件转换为多种格式,包括HTML。它具有直观的操作界面和简单易用的操作流程,即使对于初学者也很友好。
【转换步骤】
步骤一:打开软件后,选择“PDF转HTML”功能。
步骤二:添加PDF文件,接着调整“页码选择”,然后点击“开始转换”即可。
二、使用HODO将PDF转HTML
这是一个受欢迎的PDF转HTML平台,它专注于提供出色的转换质量和准确性。它的转换引擎使用先进的算法,可以保留原始PDF文件的布局和格式,并将其转换为高质量的HTML文件。
【转换步骤】
步骤一:使用浏览器打开平台的网站后,选择“PDFto HTML”功能。
步骤二:添加PDF文件,接着平台就会自动识别并进行转换。
三、使用ZAMZAR将PDF转HTML
这是另一个在线文件转换平台,提供多种文件格式之间的转换服务,包括PDF到HTML的转换。这个平台非常方便使用,我们不需要安装任何软件。
【转换步骤】
步骤一:打开平台后,通过“ChooseFiles”上传PDF文件。
步骤二:在“WebFormats”中选择“HTML”格式,接着点击“ConvertNow”就能完成转换。
看完这篇文章,大家都学会PDF怎么转HTML了吗?以上三种方法都可以很好地实现PDF到HTML的转换,帮助我们利用PDF内容在网页中发挥更广泛的价值。具体选择哪种方法,需要大家结合自身实际转换需求来定。
spose.PDF for .NET是一种高级PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。此外,还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
HTML到PDF的转换在将不同文件格式相互转换之间具有其自身的意义,可以使用其他可用的应用程序,工具和在线服务将HTML转换为PDF。同样,我们也可以使用编程的方式,将HTML转换为PDF。
在Aspose.PDF for .Net中,提供了免费的HTML到PDF的基本转换,而且还允许指定各种选项来实现所需的功能,比如将网页转换为PDF、使用SVG数据渲染HTML等等。接下来我们一起通过示例解读的方式学习如何实现这些功能。
只需使用几行代码和资源加载回调就可以以非常基本的方式将HTML转换为PDF,以下是使您达到目的的代码段:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); HtmlLoadOptions options = new HtmlLoadOptions(); options.CustomLoaderOfExternalResources = new LoadOptions.ResourceLoadingStrategy(SamePictureLoader); Document pdfDocument = new Document(dataDir + "HTMLToPDF.html", options); pdfDocument.Save("HTMLToPDF_out.pdf"); private static LoadOptions.ResourceLoadingResult SamePictureLoader(string resourceURI) { string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); byte[] resultBytes = File.ReadAllBytes(dataDir + "aspose-logo.jpg"); LoadOptions.ResourceLoadingResult result = new LoadOptions.ResourceLoadingResult(resultBytes); return result; }
通常需要将网页转换为PDF,并且如果手动执行此操作,则需要执行多个步骤。API提供的功能可以使用下面显示的代码执行。需要注意的是,以下代码段涵盖了Web页面到PDF转换操作的两个主要和基本方面:
//文档目录的路径。 string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); //创建对该URL的请求。 WebRequest request = WebRequest.Create("https:// En.wikipedia.org/wiki/Main_Page"); //如果服务器需要,请设置凭据。 request.Credentials = CredentialCache.DefaultCredentials; //在请求超时之前以毫秒为单位超时 // Request.Timeout = 100; //获取响应。 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获取包含服务器返回内容的流。 Stream dataStream = response.GetResponseStream(); //使用StreamReader打开流以方便访问。 StreamReader reader = new StreamReader(dataStream); //阅读内容。 string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer)); HtmlLoadOptions options = new HtmlLoadOptions("https:// En.wikipedia.org/wiki/"); //加载HTML文件 Document pdfDocument = new Document(stream, options); options.PageInfo.IsLandscape = true; //将输出另存为PDF格式 pdfDocument.Save(dataDir + "WebPageToPDF_out.pdf");
以下代码段显示了如何将带有SVG图形标签的HTML文件转换为Tagged PDF Document:
//文档目录的路径 string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion(); //设置输入文件路径 string inFile = dataDir + "HTMLSVG.html"; //设置输出文件路径 string outFile = dataDir + "RenderHTMLwithSVGData.pdf"; //初始化HtmlLoadOptions HtmlLoadOptions options = new HtmlLoadOptions(Path.GetDirectoryName(inFile)); //初始化Document对象 Document pdfDocument = new Document(inFile, options); //保存 pdfDocument.Save(outFile);
点击下方“了解更多”免费下载最新版Aspose.PDF for .NET
↓↓↓
要使用的是wkhtmltopdf的Python封装——pdfkit
安装
1. Install python-pdfkit:
$ pip install pdfkit
2. Install wkhtmltopdf:
$ sudo apt-get install wkhtmltopdf
sudo yum intsall wkhtmltopdf
brew install Caskroom/cask/wkhtmltopdf
使用
一个简单的例子:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
你也可以传递一个url或者文件名列表:
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')
也可以传递一个打开的文件:
with open('file.html') as f:
pdfkit.from_file(f, 'out.pdf')
如果你想对生成的PDF作进一步处理, 你可以将其读取到一个变量中:
# 设置输出文件为False,将结果赋给一个变量
pdf = pdfkit.from_url('http://google.com', False)
你可以制定所有的 wkhtmltopdf 选项 http://wkhtmltopdf.org/usage/wkhtmltopdf.txt. 你可以移除选项名字前面的 '--' .如果选项没有值, 使用None, False or * 作为字典值:
options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None
}
pdfkit.from_url('http://google.com', 'out.pdf', options=options)
默认情况下, PDFKit 将会显示所有的 wkhtmltopdf 输出. 如果你不想看到这些信息,你需要传递一个 quiet 选项:
options = {
'quiet': ''
}
pdfkit.from_url('google.com', 'out.pdf', options=options)
由于wkhtmltopdf的命令语法 , TOC 和 Cover 选项必须分开指定:
toc = {
'xsl-style-sheet': 'toc.xsl'
}
cover = 'cover.html'
pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)
当你转换文件、或字符串的时候,你可以通过css选项指定扩展的 CSS 文件。
# 单个 CSS 文件
css = 'example.css'
pdfkit.from_file('file.html', options=options, css=css)
# Multiple CSS files
css = ['example.css', 'example2.css']
pdfkit.from_file('file.html', options=options, css=css)
你也可以通过你的HTML中的meta tags传递任意选项:
body = """
<html>
<head>
<meta name="pdfkit-page-size" content="Legal"/>
<meta name="pdfkit-orientation" content="Landscape"/>
</head>
Hello World!
</html>
"""
pdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape
配置
每个API调用都有一个可选的参数。这应该是pdfkit.configuration()API 调用的一个实例. 采用configuration 选项作为初始化参数。可用的选项有:
示例 :针对wkhtmltopdf不在系统路径中(不在$PATH里面)
PATH里面):
config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))
pdfkit.from_string(html_string, output_file, configuration=config)
问题
IOError:'No wkhtmltopdf executable found':
确保 wkhtmltopdf 在你的系统路径中(PATH), 会通过 configuration进行了配置 (详情看上文描述)。 在Windows系统中使用where wkhtmltopdf命令 或 在 linux系统中使用 which wkhtmltopdf 会返回 wkhtmltopdf二进制可执行文件所在的确切位置.
如果出现这个错误意味着 PDFKit不能处理一个输入。你可以尝试直接在错误信息后面直接运行一个命令来查看是什么导致了这个错误 (某些版本的 wkhtmltopdf会因为段错误导致处理失败)
确保两项:
1)、你的系统中有中文字体
2)、在html中加入
下面是我随便写的一个HTML表格:
<html>
<head><meta charset="UTF-8"></head>
<body>
<table width="400" border="1">
<tr>
<th align="left">Item....</th>
<th align="right">1</th>
</tr>
<tr>
<td align="left">衣服</td>
<td align="right">1.10</td>
</tr>
<tr>
<td align="left">化妆品</td>
<td align="right">.00</td>
</tr>
<tr>
<td align="left">食物</td>
<td align="right">0.40</td>
</tr>
<tr>
<th align="left">tOTAL</th>
<th align="right">01.50</th>
</tr>
</table>
</body>
</html>
下面是生成的PDF截图
*请认真填写需求信息,我们会在24小时内与您取得联系。