Cuppyzh
Cuppyzh

Reputation: 142

HTML to PDF Page fit to HTML

Currently I was working on feature to convert html to pdf using itext7. But I got stuck when I try to fit the pdf page size into html content.

My expectation is to remove these space so the pdf will appear like the html.

enter image description here

This is how the html looks like

enter image description here

This is the code that I currently use

private MemoryStream _GenerateFile(string html)
    {
        ConverterProperties converterProperties = new ConverterProperties();
        converterProperties.SetImmediateFlush(false);
        converterProperties.SetBaseUri(""); 

        MemoryStream stream = new MemoryStream();
        PdfWriter writer = new PdfWriter(stream);
        PdfDocument pdf = new PdfDocument(writer);
        pdf.SetDefaultPageSize(PageSize.A4);

        HtmlConverter.ConvertToPdf(html, pdf, converterProperties);
        var pdfBytes = stream.ToArray();
        pdf.Close();

        MemoryStream result = new MemoryStream(pdfBytes);
        result.Position = 0;

        return result;
    }

Things I've tried

Feel free to drop any suggestion here, Thanks.

Update Thanks for the suggestions, I've tried all the suggestion is not solve my issue. Meanwhile on the half way we decide to change from itext7 to as aspose instead. So this issue is not relevant anymore.

Upvotes: 0

Views: 1346

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12312

The following CSS instruction sets the margins of the page to 0:

<style>
    @page {
      margin: 0;
    }
</style>

Upvotes: 1

Related Questions