我正在为目录动态生成HTML代码,我也希望生成PDF。我认为只是将HTML页面打印到PDF文档,但是我丢失了一些背景阴影和内容,并且它会跨页面拆分内容。
我已经阅读了一些关于iText的内容,但我还没有弄清楚如何正确地格式化它,我不知道如何制作它以便它不会在页面之间分割我的内容。
这是我的HTML页面的开头,我包含了几个项目,因此您可以看到内容是如何分解的。我为丑陋的HTML道歉,我不能为我的生活得到一个div表看起来正确!
<style type="text/css">
<!--
tr#odd {
background-color:#e2e2e2;
vertical-align:top;
}
tr#even {
vertical-align:top;
}
div#title {
font-size:16px;
font-weight:bold;
}
div#mpaa {
font-size:10px;
}
div#genre {
font-size:12px;
font-style:italic;
}
div#plot {
height: 63px;
font-size:12px;
overflow:hidden;
}
-->
</style>
<html>
<title>Movie Catalog</title>
<body>
718 Movies
<br />
<br />
<table>
<tr id="odd">
<td>
<img src=".\images\10,000BCDVDrip.jpg" width="75" height="110">
</td>
<td>
<div id="title">10,000 BC</div>
<div id="mpaa"> </div>
<div id="genre">Adventure, Drama</div>
<div id="plot">A prehistoric epic that follows a young mammoth hunter's journey through uncharted territory to secure the future of his tribe.</div>
</td>
</tr>
<tr id="even">
<td>
<img src=".\images\101Dalmatians1961PlatinumEditionDVDRipXviD.jpg" width="75" height="110">
</td>
<td>
<div id="title">101 Dalmatians (Platinum Edition)</div>
<div id="mpaa">G </div>
<div id="genre">Comedy, Family, Disney</div>
<div id="plot">The Live action adaptation of a Disney Classic. When a litter of dalmatian puppies are abducted by the minions of Cruella De Vil, the parents must find them before she uses them for a diabolical fashion statement.</div>
</td>
</tr>
<tr id="odd">
<td>
<img src=".\images\102DalmationsDVDrip.jpg" width="75" height="110">
</td>
<td>
<div id="title">102 Dalmations</div>
<div id="mpaa">G </div>
<div id="genre">Family</div>
<div id="plot">After a spot of therapy Cruella De Vil is released from prison a changed woman. Devoted to dogs and good causes, she is delighted that Chloe, her parole officer, has a dalmatian family and connections with a dog charity. But the sound of Big Ben can reverse the treatment so it is only a matter of time before Ms De Vil is back to her incredibly ghastly ways, using her new-found connections with Chloe and friends</div>
</td>
</tr>
<tr id="even">
<td>
<img src=".\images\127Hours2010720pBluRayx264.jpg" width="75" height="110">
</td>
<td>
<div id="title">127 Hours</div>
<div id="mpaa">R Rated R for language and some disturbing violent content/bloody images.</div>
<div id="genre">Action, Adventure, Drama, Suspense, Thriller</div>
<div id="plot">127 Hours is the true story of mountain climber Aron Ralston's (James Franco) remarkable adventure to save himself after a fallen boulder crashes on his arm and traps him in an isolated canyon in Utah. Over the next five days Ralston examines his life and survives the elements to finally discover he has the courage and the wherewithal to extricate himself by any means necessary, scale a 65 foot wall and hike over eight miles before he is finally rescued. Throughout his journey, Ralston recalls friends, lovers (Clemence Poesy), family, and the two hikers (Amber Tamblyn and Kate Mara) he met before his accident. Will they be the last two people he ever had the chance to meet?</div>
</td>
</tr>
<tr id="odd">
<td>
<img src=".\images\13GoingOn30DVDrip.jpg" width="75" height="110">
</td>
<td>
<div id="title">13 Going On 30</div>
<div id="mpaa">PG-13 for some sexual content and brief drug references</div>
<div id="genre">Comedy, Fantasy, Romance</div>
<div id="plot">After total humiliation at her thirteenth birthday party, Jenna Rink wants to just hide until she's thirty. Thanks to some wishing dust, Jenna's prayer has been answered. With a knockout body, a dream apartment, a fabulous wardrobe, an athlete boyfriend, a dream job, and superstar friends, this can't be a better life. Unfortunetly, Jenna realizes that this is not what she wanted. The only one that she needs is her childhood best friend, Matt, a boy that she thought destroyed her party. But when she finds him, he's a grown up, and not the same person that she knew.</div>
</td>
</tr>
...
...
</table>
</body>
</html>
您可以看到它的外观:http://timelessdesigncafe.com/movies/catalog.html 请注意,背景阴影交替显示。当我打印到PDF时,我失去了阴影,更重要的是,它在两页上吐出“行”/电影,我需要避免这种情况。
提前致谢!!
答案 0 :(得分:6)
没人提到wkhtmltopdf? :)
答案 1 :(得分:0)
您可以按照代码中的以下步骤使用OpenOffice API执行此转换:
我知道它适用于VB(已经在VBScripts中使用它),C ++和Java,你应该可以用C#做同样的事情。
链接:
http://www.kalitech.fr/clients/doc/VB_APIOOo_en.html
http://wiki.services.openoffice.org/wiki/API/Tutorials/PDF_export
答案 2 :(得分:0)
有太多方法可以做到。请查看this主题。 如果您想使用免费的库或工具,可以使用iTextSharp,但免费版不能满足所有要求。因此,您可以使用其他工具,例如ABCPdf
答案 3 :(得分:0)
如果您可以使用WPF,则可能需要考虑使用FixedDocument
并在XAML中进行打印。然后,您可以将XAML(利用数据绑定,如果适用)栅格化为XPS,即用于文档布局的Microsoft XML Paper Standard(基本上是他们的PDF版本)。
这种方法的优点是能够利用数据绑定和XAML(恕我直言)优越(到HTML)布局功能。我一直在使用这个堆栈作为轻量级报告解决方案。 (您需要在STA线程上生成报告)。
下一步(是的,这可能有点复杂)然后将您的XPS流通过某些转换器传递到PDF格式,但不确定是否存在这样的事情。否则,您将依赖于拥有XPS阅读器的客户(尽管这是内置于最新版本的Windows和Office)。
答案 4 :(得分:0)
正确布局HTML是一项非常重要的任务。我估计可能需要一年或两年的时间才能做到正确。
所以这不是要走的路。相反,您应该过滤数据的HTML,然后编写一个小的,专用的PDF格式化程序,它可以完全满足您的需要,即使输入HTML中的最小变化也会中断。
这应该需要一个星期左右的时间。完成后,使其更适应输入HTML中的更改。
答案 5 :(得分:0)
如果您不介意花一点钱,可以投资PrinceXML,将任何Xml文档(包括XHtml)格式化为.pdf文档,将完整的布局规则应用于Html内容。实际上,在进行布局传递时,Prince比许多Web浏览器更符合Web标准:)
答案 6 :(得分:0)
查看WebToPDF.NET这是一个用C#编写的.NET组件,它将HTML转换为PDF。您将获得一个与HTML文件完全相同的pdf文件。我相信能够指定页面大小,您可以使用它来指定一个非常长的页面,以便在一个页面上获取所有内容。
转换器支持HTML 4.01,XHTML 1.0,XHTML 1.1和CSS 2.1,包括分页符,表单和链接。它通过了所有W3C测试(BIDI除外)。