如何在c#中生成与HTML代码相同布局的动态PDF

本文关键字:代码 布局 PDF 动态 HTML | 更新日期: 2023-09-27 18:01:45

我正在动态地为一个目录生成HTML代码,我还想生成一个PDF。我考虑过只是将HTML页面打印成PDF文档,但是我失去了一些背景阴影和东西,并且它将内容拆分到各个页面。

我读了一些关于ittext的内容,但是我还没有弄清楚如何正确地格式化它,而且我不知道如何使它不会跨页面拆分我的内容。

这是我的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格式时,我失去了阴影,更重要的是,它在两页上吐出"行"/电影,我需要避免这种情况。

提前感谢!!

如何在c#中生成与HTML代码相同布局的动态PDF

没有人提到过wkhtmltopdf?:)

您可以使用OpenOffice API来完成此转换,在代码中执行以下步骤:

  • 加载OpenOffice API
  • 打开HTML文件
  • 保存为PDF

我知道它适用于VB(已经在VBScripts中使用过),c++和Java,你应该能够用c#做同样的事情。

链接:
http://www.kalitech.fr/clients/doc/VB_APIOOo_en.htmlhttp://wiki.services.openoffice.org/wiki/API/Tutorials/PDF_export

你有很多方法可以做到这一点。请检查这个主题。如果你想使用免费的库或工具,你可以使用iTextSharp,但免费版本并不能满足所有的要求。你可以使用其他工具比如ABCPdf

正确布局HTML是一项非常重要的任务。我估计可能需要一到两年的时间才能把它做好。

所以这不是我们要走的路。相反,您应该为数据过滤HTML,然后编写一个小型的专用PDF格式化程序,它可以完全满足您的需求,甚至可以在输入HTML中进行最小的更改。

那应该需要一周左右的时间。当你完成了这些,让它更适应输入HTML的变化。

如果你在使用WPF的位置,你可能要考虑使用FixedDocument和做你的布局在XAML打印。然后,您可以将XAML栅格化(如果合适的话,利用数据绑定)到XPS,即微软用于文档布局的XML Paper Standard(本质上是他们的PDF版本)。

这种方法的优点是能够利用数据绑定和XAML (IMHO)优于HTML的布局功能。我使用这个堆栈作为轻量级报告解决方案已经有一段时间了。(您需要在STA线程上生成报告)。

下一步(是的,这可能变得有点复杂)是将XPS流通过某些转换器转换为PDF格式,但不确定是否存在这样的事情。否则,您将依赖于您的客户拥有XPS阅读器(尽管这是内置在最新版本的Windows &办公室).

如果您不介意花一点钱,您可以投资于prinexml,它将任何Xml文档(包括XHtml)格式化为.pdf文档,并对Html内容应用完整的布局规则。事实上,Prince在布局时比许多浏览器更符合web标准:)

看一下WebToPDF。它是一个用c#编写的。NET组件,可以将HTML转换为PDF。您将得到一个pdf文件,看起来与HTML文件完全相同。我相信有能力指定页面大小,你可以用它来指定一个很长的页面,把所有的东西都放在一个页面上。

转换器支持HTML 4.01、XHTML 1.0、XHTML 1.1和CSS 2.1,包括分页符、表单和链接。它通过了所有W3C测试(BIDI除外)。