0

I did a simple html to pdf conversion getting a landscape orientation. In the pdfHtml release notes I see that the default orientation should be portrait but I got a landscape.

I'm not able to find the option/parameter/setting to do it.

Probably it is in the ConverterProperties object hidden to my eyes :-(

Any suggestion?

Here is my very simple code

public byte[] HtmlToPdf(string html)
{
    using (Stream htmlSource = new MemoryStream(Encoding.UTF8.GetBytes(html)))
    using (MemoryStream pdfDest = new MemoryStream())
    {
        ConverterProperties converterProperties = new ConverterProperties();
        HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);

        return pdfDest.ToArray();
    }
}

EDIT after answers (I got the right orientation!):

Now I have to find a way to scale down the content in order to fit the content and have the right margins without cutting the image.

public static byte[] HtmlToPdf(string html)
{
    using (Stream htmlSource = new MemoryStream(Encoding.UTF8.GetBytes(html)))
    using (MemoryStream pdfDest = new MemoryStream())
    {
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter(pdfDest));
        pdfDocument.SetDefaultPageSize(PageSize.A4.Rotate());
        ConverterProperties converterProperties = new ConverterProperties();
        HtmlConverter.ConvertToPdf(htmlSource, pdfDocument, converterProperties);

        return pdfDest.ToArray();
    }
}

HTML result:

enter image description here

PDF result:

enter image description here

Tonyc
  • 709
  • 1
  • 10
  • 29
  • 1
    quick comment, the default orientation has only changed if you're using a ledger page size – André Lemos Jun 22 '20 at 08:20
  • 1
    Does this answer your question? [How to set orientation to Landscape in iText 7](https://stackoverflow.com/questions/54347293/how-to-set-orientation-to-landscape-in-itext-7) – André Lemos Jun 22 '20 at 08:23
  • @AndréLemos, both answers have been useful, I'm new on this topic. So I edited the text with the current result. Is there a way to scale in order to fit the content and have the margin on the right side without cutting the content? Do I have to work on the HTML or on the itext7 conversion configuration? – Tonyc Jun 23 '20 at 06:52
  • 1
    well, there are multiple ways to go around it. look here https://stackoverflow.com/questions/3341485/how-to-make-a-html-page-in-a4-paper-size-pages and https://stackoverflow.com/questions/47869248/page-size-and-formatting-of-pdf-using-itext-pdfhtml – André Lemos Jun 23 '20 at 07:58
  • @AndréLemos thanks for the really good related articles. I did some test but it's too hard in my context to find the right way. The html is produced by an external application, so I should transform it. These articles are very good to understand that it's hard and in addition itext7 produces a couple of unexpected white lines in the rendering. I found a good solution changing the library from itext7 to dinkToPdf based on wkhtmltopdf, with the result I attached that could be useful to other developers... maybe – Tonyc Jun 27 '20 at 12:43

1 Answers1

0

I solved changing the library from itext7 to DinkToPdf. I found it very simple to use and enough to my needs.


The MVC controller after changing the library to DinkToPdf

public byte[] PdfCreatorController(IConverter converter)
    string html = await reader.ReadToEndAsync();

    var globalSettings = new GlobalSettings
    {
        ColorMode = ColorMode.Color,
        Orientation = Orientation.Portrait,
        PaperSize = PaperKind.A4,
        Margins = new MarginSettings { Top = 10 },
        DocumentTitle = "Report",    
        Out = string.Empty,
    };

    var objectSettings = new ObjectSettings
    {
        PagesCount = true,
        HtmlContent = html,
        WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },
    };

    var pdf = new HtmlToPdfDocument()
    {
        GlobalSettings = globalSettings,
        Objects = { objectSettings }
    };

    return = _converter.Convert(pdf);
}

PDF Result

enter image description here

Tonyc
  • 709
  • 1
  • 10
  • 29