Free Support Forum - aspose.com

Convert HTML to Letter Sized PDF


#1

I’m trying to create a Letter-sized PDF from an HTML document and I must be missing something really simple because it’s not coming out the correct size.

If I use the default PageSetup in the Aspose.Html.Rendering.Pdf.PdfRenderingOptions object, I get a 8.27in x 11.69in sized page in my PDF - that is A4 paper size (210mmx297mm).

I want 8.50in x 11in (216mm x 279mm).

The PageSetup.AnyPage.Size object takes in a width and height in pixels as integer, or two length objects.

I tried Aspose.Html.Drawing.Size(850, 1100) - this gives me the incorrect output of 8.85" x 11.46", as you asked for pixels I expected this not to work.

I tried passing points (Aspose.Html.Drawing.Size(612, 792) – 72dpi x paper size in inches), this too resulted in the incorrect paper size (6.28" x 8.25"), as I expected.

The default resolution on the PageSetup object is 300 dpi, so I tried settings pixels as the dpi * page dimensions, new Aspose.Html.Drawing.Size(2250, 3300); again, this gave the incorrect page size as 23in x 34in… definitely not what I wanted.

In all of the above tests I was also passing in a margin of 0, Aspose.Html.Drawing.Margin(0). but I was getting the same (or similar) results without passing this in. I used that setting to test if if the margins were affecting my output.

What settings do I need to get a letter sized page? All of your sample code for HTML to PDF conversion is not using Paper sizes, but simple samples like 200x 100 pixels. Most of our clients are looking for Letter sized pages, like if they printed to a printer.

Thanks!


#2

Never mind, I stumbled upon the answer looking for other information in the forums:

// Letter Paper size, in pixels
pdf_options.PageSetup.AnyPage =  new Aspose.Html.Drawing.Page(
    new Aspose.Html.Drawing.Size(Aspose.Html.Drawing.Unit.FromInches(8.5),
                                 Aspose.Html.Drawing.Unit.FromInches(11)),
                                 new Aspose.Html.Drawing.Margin(0) );

#3

@sheri_steeves

Thank you for your kind feedback.

We are glad to know that you have found the solution. Please keep using our API and in event of any further query, feel free to ask.