Click here to Skip to main content
15,888,251 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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();



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.
Posted
Comments
Maciej Los 6-Feb-17 11:17am    
As to the 5. - go to sleep, rest and get back to work ;)
SO: Convert HTML to PDF in .NET
FoxRoot 7-Feb-17 4:29am    
Thanks :) However, I have no access install something on server:/
[no name] 7-Feb-17 21:47pm    
FoxRoot 8-Feb-17 4:53am    
Thank you. I am going to read after some sleep :)

1 solution

 
Share this answer
 
Comments
FoxRoot 7-Feb-17 4:29am    
Thanks. I am using VS2010, MVC 3
sonymon mishra 7-Feb-17 5:01am    
What are you using, Razor or ASPX as your view engine?
FoxRoot 8-Feb-17 4:53am    
Thanks for your interest. I am using Razor.
sonymon mishra 9-Feb-17 0:00am    
Hope this helps
https://www.codeproject.com/Questions/1044376/Export-MVC-Razor-View-to-pdf-without-iTextSharp-Ra

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900