htmldoc能够将html转换成PDF文件

htmldoc能够将html转换成PDF文件
2007年07月12日 星期四 09:17

htmldoc能够将html转换成PDF文件

htmldoc 包括2个版本,开源版本和商业版本。 ubuntu上是可直接使用 sudo apt-get install htmldoc 安装的。 Windows上的用户如果不想出点血,就得自己去编译了吧...

htmldoc有个很好的web界面,但是我主要是想在服务器上使用,因此我对其命令行使用更感兴趣。

要将页面filename.html,转换成PDF,执行这个就可以了:

htmldoc --webpage -f output.pdf filename.html

要将file1.html, file2.html组装成一个分页的PDF,可执行这个:

htmldoc --webpage -f output.pdf file1.html file2.html

最酷的在这里了,要把slashdot.org的首页转换成PDF,很轻松:

htmldoc --webpage -f output.pdf http://slashdot.org/

想制作一本带目录的书,很容易:

htmldoc --book -f output.pdf file1.html file2.html

想增加一个漂亮的图片做封皮?这样的:

htmldoc --book -f 12book.pdf 1book.html 2book.html --titlefile bookcover.jpg

说了这么多好,最大的问题却遗漏了,支持中文吗?非常遗憾,还不支持UTF-8/Unicode/Chinese/Japanese。看到有文章说1.9版本会支持UTF8,1.9也还没正式release. 具体如何,还不得而知。

又想起了 ReportLab , 2.0版本最大的特性就是支持unicode,但是我现在已经忘记上次评测的结果了。

中文PDF生成是心中永远的痛。

对了,前2天研究了下openoffice的转换机制,有个oooconv的产品,在这里:

https://infrae.com/svn/buildout/oooconv-dev/trunk/

但是它只能在Linux上运行,而且安装很麻烦,不过这个方法还是很值得跟踪的。

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将HTML格式文本换为PDF文件,您可以使用开源的Java库iText。以下是一个简单的示例代码: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfWriter; import org.jsoup.Jsoup; import org.jsoup.helper.W3CDom; import org.w3c.dom.Document; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class HtmlToPdfConverterExample { public static void main(String[] args) { String inputFilePath = "input.html"; String outputFilePath = "output.pdf"; try { File outputFile = new File(outputFilePath); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); document.open(); org.jsoup.nodes.Document htmlDoc = Jsoup.parse(new File(inputFilePath), "UTF-8"); org.w3c.dom.Document w3cDoc = new W3CDom().fromJsoup(htmlDoc); com.itextpdf.tool.xml.XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(inputFile)); document.close(); System.out.println("PDF created successfully!"); } catch (IOException | DocumentException e) { e.printStackTrace(); } } } ``` 在运行代码之前,请确保已将iText库添加到您的项目中。此代码将读取名为`input.html`的HTML文件,并将其换为名为`output.pdf`的PDF文件。 请注意,这只是一个简单的示例,对于包含复杂样式和布局的HTML文件,可能需要额外的处理和调整来保留正确的格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值