0

I am using itext7 pdfhtml (4.0.3) to convert Html to pdf in memory. Below method is taking html in memory and returning PdfDocument object of itext7. I need to convert that PdfDocument object to byte array or stream. Please let me know how we can achieve that.

 private iText.Kernel.Pdf.PdfDocument CreatePdf( string html)
    {
        byte[] bytes = Encoding.ASCII.GetBytes(html);
        ConverterProperties properties = new ConverterProperties();
        properties.SetBaseUri(path);
       
        MemoryStream myMemoryStream = new MemoryStream(bytes);
        PdfWriter writer = new(myMemoryStream);
        iText.Kernel.Pdf.PdfDocument pdf = new iText.Kernel.Pdf.PdfDocument(writer);
        pdf.SetDefaultPageSize(PageSize.A4);
        pdf.SetTagged();
        HtmlConverter.ConvertToDocument(html,pdf,properties);
       
        return pdf;
    }
jps
  • 20,041
  • 15
  • 75
  • 79
  • 1
    Does this answer your question? [Itext7 HtmlConverter does not display gif](https://stackoverflow.com/questions/69210758/itext7-htmlconverter-does-not-display-gif) – Tu deschizi eu inchid Oct 12 '22 at 03:44
  • 1
    You can retrieve the bytes from the `myMemoryStream` you initialized the `PdfWriter` with. But you must not initialize that memory stream with the HTML bytes. Your current code produces a mix of HTML and pdf in there. – mkl Oct 12 '22 at 04:46

1 Answers1

1

to @mkl's point, you're kind of overdoing it. Here's a simple example:

var html = "<h1>hi mom</h1>";

byte[] result;

using (var memoryStream = new MemoryStream())
{
    var pdf = new PdfDocument(new PdfWriter(memoryStream));
    pdf.SetDefaultPageSize(PageSize.A4);
    pdf.SetTagged();
    HtmlConverter.ConvertToPdf(html, pdf, new ConverterProperties());
    result = memoryStream.ToArray();
}

File.WriteAllBytes(@"/tmp/file.pdf", result);

the memoryStream will have your in memory representation of your conversion. I've added the WriteAllBytes bits just so you can see for yourself.

Another note, if you do not require setting any PdfDocument properties, you can use an even simpler version:

var html = "<h1>hi mom</h1>";

byte[] result;

using (var memoryStream = new MemoryStream())
{
    HtmlConverter.ConvertToPdf(html, memoryStream);
    result = memoryStream.ToArray();
}

File.WriteAllBytes(@"/tmp/file.pdf", result);
André Lemos
  • 837
  • 6
  • 18