Search code examples
c#openhtmltopdf

OpenHTMLtoPDF Custom Page Size


I am trying to set a custom page size with OpenHTMLtoPDF. I am trying to convert an HTML source to a 58mm x 110mm thermal paper print job, however, I got stuck on this.

I have tried setting the page size directly like so:

var pdf = Pdf.From(html).OfSize(58,110);//Invalid arguments 

and

var pdf = Pdf.From(html).OfSize(new PaperSize(58,110));//Invalid arguments

Solution

  • You are defining the size incorrectly.

    What you need to do is create a new paper size like so:

    OpenHtmlToPdf.PaperSize size = new OpenHtmlToPdf.PaperSize(Length.Millimeters(58), Length.Millimeters(110));
    

    Then to set your PDF document to have the right paper size you would call this like so

    var pdf = Pdf.From(html).OfSize(size);
    

    Side Note

    If you are using a different unit of measurement you can define what unit of measurement you are using like so:

    Length.Inches(MeasurementInInches);
    Length.Centimeters(MeasurementInCentimeters);