如何将HTML内容转换为带有图像的pdf [英] How can I convert HTML content to pdf with images

查看:64
本文介绍了如何将HTML内容转换为带有图像的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Asp.Net MVC 3.我在db中有一些html内容。此内容包含一些图像。我想生成pdf。文字还可以。但是我无法检索图像。我尝试了不同的应用程序,如Rotativa,ITextSharp等。



Rotativa在本地工作正常,但是在产品上。服务器它失败了。我无权在产品上安装Rotativa。



我在几周内搜索过,ITextSharp工作正常。但是无法检索图像。只需发布文字即可。有没有办法发布所有元素(表格,图像等)的Html内容



I am working with Asp.Net MVC 3. I have some html content in db. This content contains some images. I am trying to generate pdf. Texts are okay. However I am failing to retrieve images. I tried different apps such as Rotativa, ITextSharp etc.

Rotativa works fine on local, however on prod. server it's failing. I have no permission to install Rotativa on prod.

I searched during weeks and ITextSharp works fine. However can' t retrieve images. Just publish the text. Is there any way to publish Html content with all elements(tables, images etc.)

<pre lang="c#">

<pre>  public byte[] GetPDF(string pHTML)
        {

            byte[] bPDF = null;

            MemoryStream ms = new MemoryStream();
            TextReader txtReader = new StringReader(pHTML);

            // 1: create object of a itextsharp document class
            Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

            // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
            PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
            
            Dictionary<string, object> interfaceProps = new Dictionary<string, object>() { 
            {"img_provider", new CustomItextImageProvider()}
        };


            // 3: we create a worker parse the document
            HTMLWorker htmlWorker = new HTMLWorker(doc);
            htmlWorker.InterfaceProps = interfaceProps;
            // 4: we open document and start the worker on the document

            doc.Open();
            htmlWorker.StartDocument();

            // 5: parse the html into the document
            htmlWorker.Parse(txtReader);

            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            bPDF = ms.ToArray();

            return bPDF;
        }

        public void DownloadPDF(int Id = 0)
        {
            var report= _report.Select(Id);
            string HTMLContent = "";
            HTMLContent += 
                "<div style=text-align:center; width:60%;'><h2>" + report.Heading+ "</h2></div>" +
                   "<br/><br/><br/><br/>" +
                   "<p>" + report.Content+ "</p>" +
                 "<center>Center this text!</center>";

            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=" + "PDFfile.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(GetPDF(HTMLContent));
            Response.End();







我的尝试:



1-在15天内在网上搜索。

2-阅读越来越多的内容。

3-向朋友询问。

4-多喝咖啡。

5-不要睡3天。




What I have tried:

1- Searched on the web during 15 days.
2- Read more and more content.
3- Asked to friends.
4- Drunk more coffee.
5- Didin' t sleep 3 days.

推荐答案

检查一下。

转换自CSharp VB和ASP.NET中的HTML到PDF,C#中的免费第三方库,Visual Studio 2012的VB.NET [ ^ ]


这篇关于如何将HTML内容转换为带有图像的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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