Jordan Ferr
Jordan Ferr

Reputation: 319

Generate Base64 from iTextSharp

I have the following method in my project.

        [Route("GerarPdf")]
        [HttpGet()]
        public object GerarPdf()
        {

            var doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            var mem = new MemoryStream();

            PdfWriter wri = PdfWriter.GetInstance(doc, mem);

            doc.Open();//Open Document to write
            Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
            Phrase pharse = new Phrase("This is my second line using Pharse.");
            Chunk chunk = new Chunk(" This is my third line using Chunk.");

            doc.Add(paragraph);
            doc.Add(pharse);
            doc.Add(chunk);

            var pdf = mem.ToArray();

            return Convert.ToBase64String(pdf);
        }

The objective of this code is generate a PDF file downloadable by the following JavaScript code

     var dataURI = "data:application/pdf;base64," +result;
     window.open(dataURI,'_blank'); 

But the new opened page return always a error on load PDF. The base64 code returned by the method to the result variable is:

JVBERi0xLjQKJeLjz9MK

Can someone help me to solve this problem?

Upvotes: 2

Views: 3835

Answers (2)

Filburt
Filburt

Reputation: 18061

To have your MemoryStream and Document automatically closed + disposed for you, put them into using() blocks like this:

[Route("GerarPdf")]
[HttpGet()]
public object GerarPdf()
{
    byte[] pdf = new byte[] { };

    using (var mem = new MemoryStream())
    {
        using (var doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35))
        {
            PdfWriter wri = PdfWriter.GetInstance(doc, mem);

            doc.Open(); //Open Document to write

            Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
            Phrase pharse = new Phrase("This is my second line using Pharse.");
            Chunk chunk = new Chunk(" This is my third line using Chunk.");

            doc.Add(paragraph);
            doc.Add(pharse);
            doc.Add(chunk);
        } // doc goes out of scope and gets closed + disposed

        pdf = mem.ToArray();
    } // mem goes out of scope and gets disposed

    return Convert.ToBase64String(pdf);
}

Upvotes: 1

Jordan Ferr
Jordan Ferr

Reputation: 319

The problem was that the doc was not closed in the moment of conversion to bytearray. After I added doc.Close(); the PDF was successfully generated.

        [Route("GerarPdf")]
        [HttpGet()]
        public object GerarPdf()
        {

            var doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            var mem = new MemoryStream();

            PdfWriter wri = PdfWriter.GetInstance(doc, mem);

            doc.Open();//Open Document to write
            Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
            Phrase pharse = new Phrase("This is my second line using Pharse.");
            Chunk chunk = new Chunk(" This is my third line using Chunk.");

            doc.Add(paragraph);
            doc.Add(pharse);
            doc.Add(chunk);

            doc.Close();

            var pdf = mem.ToArray();

            return Convert.ToBase64String(pdf);
        }

Upvotes: 2

Related Questions