使用iTextSharp的HTML转换为PDF [英] Convert HTML to PDF using itextsharp

查看:180
本文介绍了使用iTextSharp的HTML转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用HTML转换为PDF时 iTextSharp的风格与IAM CSS的网页应用在转换的PDF是行不通的。

这里是我的CSS code:

 <风格类型=文/ CSS>
       .cssformat
            {
                宽度:300像素;
                高度:200像素;
                边界:2px的纯黑色;
                背景颜色:白色;
                边框左上角的半径:60像素90像素;
                边框右下角半径:60像素90像素;
        }
        < /风格>

这里是我的html code:

 < D​​IV ID =divpdf=服务器>
        <表ID =TID=服务器>
        &所述; TR>
        &所述; TD>
       < ASP:标签ID =Label1的=服务器文本=这是PDF的新路的CssClass =cssformat>< / ASP:标签>
        < / TD>
        < / TR>
        < /表>
        < / DIV>

下面是我用C#的尝试:

  Response.ContentType =应用程序/ PDF
        Response.AddHeader(内容处置,附件;文件名= TestPage.pdf);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringBuilder的SB =新的StringBuilder();
        StringWriter的SW =新的StringWriter();
        HtmlTextWriter的HW =新的HtmlTextWriter(SW);
        文档pdfDoc =新的文档(PageSize.A4,60F,80F,-2F,35F);
        divpdf.RenderControl(HW);
        StringReader SR =新StringReader(sw.ToString());
        HTMLWorker的HTMLParser =新HTMLWorker(pdfDoc);
        PdfWriter作家= PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
        pdfDoc.Open();
        hw1.Parse(新StringReader(sttt));
        htmlparser.Parse(SR);
        pdfDoc.Close();
        的Response.Write(pdfDoc);
        到Response.End();
        sw.Close();
        sr.Close();
        hw.Close();


解决方案

我挣扎着相当多的从HTML转换为PDF使用iTextSharp的,并最终放弃了,因为我无法得到,看起来100%一样的转换的PDF我HTML5 / CSS3页面。所以,我给你,最终为我工作的替代品。

有出奇的可用很少的选项,当你不ppared支付商业库$ P $。我从我的客户(从HTML转换为PDF)是不想支付任何第三方工具的人有同样的要求,所以我不得不作出一个计划。这是我做什么,不是最好的解决办法,但把任务完成了。

我下载的 wkhtmltopdf 的最新版本。不幸的是,wkhtmltopdf工具没有显示一些转换为PDF时,嵌在我的HTML我的谷歌图形。所以我用了wkhtmltoimage工具还包括转换为PNG,这woked预期并显示所有的图形。
然后我下载 ImageMagick的的最新版本和转换的PNG为PDF。
我自动使用C#这个过程。

不幸的是,这是不是最优雅的解决方案,因为你必须进行两次转换,并做了一些工作,以自动化的一切,但是这是我能想出这给了我预期的效果和质量的最佳解决方案。

当然,也有很多的商业软件在那里,会做一个更快,更好的工作。

只是一个侧面说明:

该网页,我不得不转换成HTML5和CSS3是devloped使用自举3版本,它包含了谷歌的一些图形和图表。一切都被转换没有任何问题。

when converting html to pdf using itextsharp the style iam applying with css for the web page is not working in the converted pdf.

here is my css code :

<style type="text/css">
       .cssformat
            {
                width:300px;
                height:200px;
                border:2px solid black;
                background-color:white; 
                border-top-left-radius:60px 90px; 
                border-bottom-right-radius:60px 90px;
        }                
        </style>

here is my html code :

      <div id="divpdf" runat="server">
        <table id="tid" runat="server">
        <tr>
        <td>
       <asp:Label ID="Label1" runat="server" Text="this is new way of pdf" CssClass="cssformat"></asp:Label>
        </td>
        </tr>
        </table>
        </div>

The following is what i have tried with c# :

 Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        Document pdfDoc = new Document(PageSize.A4, 60f, 80f, -2f, 35f);
        divpdf.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());   
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        hw1.Parse(new StringReader(sttt));
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
        sw.Close();
        sr.Close();
        hw.Close();

解决方案

I struggled quite a bit to convert from HTML to PDF using iTextSharp and eventually gave up because I could not get a converted PDF that looked 100% the same as my HTML5/CSS3 page. So I'm giving you the alternative that eventually worked for me.

There is surprisingly very little options available when you are not prepared to pay for a commercial library. I had the same requirement from one of my clients(to convert from HTML to PDF) that did not want to pay for any third party tools, so I had to make a plan. This is what I did, not the best solution, but it got the job done

I downloaded the newest version of wkhtmltopdf. Unfortunately the wkhtmltopdf tool did not display some of my google graphs embedded in my HTML when converting to PDF. So I used the wkhtmltoimage tool also included to convert to a PNG, which woked as expected and displayed all the graphs. I then downloaded the newest version of imagemagick and converted the PNG to PDF. I automated this process using C#.

Unfortunately this is not the most elegant solution because you have to perform two conversions and do a bit of work to automate everything, but this is the best solution I could come up with that gave me the desired results and quality.

Of course there are lots of commercial software out there that will do a faster and better job.

Just a side note:

The web page that I had to convert was devloped in HTML5 and CSS3 using version 3 of bootstrap and it contained some google graphs and charts. Everything was converted without any problems.

这篇关于使用iTextSharp的HTML转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆