Transform Stream into PDF to send with attachment in email

4

I'm making a page for sending emails, and I need to turn a specific page into pdf to be attached to the email.

The page I have already been able to transform into Stream, now how can I make it into pdf and then attachar as an attachment.?

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Request.UrlReferrer.AbsoluteUri);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = null;
            if(response.CharacterSet == null)
            {
                readStream = new StreamReader(receiveStream);
            }
            else
            {
                readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
            }


            response.Close();
            readStream.Close();}
    
asked by anonymous 28.05.2015 / 02:52

1 answer

2

Add to your project the ItextSharp and ItextSharp.xmlworker Nuget packages

Add the following using in your code:

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline.end;
using iTextSharp.tool.xml.pipeline.html;

In my example below, I'll get an html that is in the String and display on the screen.

 #region Transformar o HTML em PDF

                using (var documentPDF = new Document(PageSize.A4, 40, 40, 40, 40))
                {
                    HttpContext.Current.Response.ContentType = "application/pdf";

                    //TextReader xhmtlStr = new StringReader(readStream.ReadToEnd());//Leio o seu HTML da Stream
                     TextReader xhmtlStr = new StringReader(@"<html><h1>Teste!</h1><p style=""backgroung:red;color:white;"">Parágrafo de Teste</p></html>");//Leio o HTML da Stream

                    var ms = new MemoryStream();
                    PdfWriter pdfWriter = PdfWriter.GetInstance(documentPDF, ms);

                    documentPDF.Open();

                    var htmlContext = new HtmlPipelineContext(null);
                    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

                    IPipeline pdfWriterPipeline = new PdfWriterPipeline(documentPDF, pdfWriter);

                    //Exporta para o PDF.
                    IPipeline htmlPipeline = new HtmlPipeline(htmlContext, pdfWriterPipeline);

                    var xmlWorker = new XMLWorker(htmlPipeline, true); //True para parse do HTML
                    var xmlParser = new XMLParser(true, xmlWorker);

                    xmlParser.Parse(xhmtlStr);
                    xmlParser.Flush();

                    documentPDF.Close();
                    documentPDF.Dispose();

                    // O MemoryStream contém seu PDF com o HTML convertido em PDF, só pegar e enviar por email.
                    //Caso queira exibir na tela, use o código abaixo
                    HttpContext.Current.Response.BinaryWrite(ms.ToArray()); //Aqui se quiser exibir o PDF na página.
                    HttpContext.Current.Response.End();
                    HttpContext.Current.Response.Flush();

                }
                #endregion

Taking the value of MemoryStream, you can now attach it in your email.

A source that can help you is also at link

    
28.05.2015 / 16:02