How do I fit-to-page/shrink-to-fit/scale-to-fit?

I would like to render to PDF on a single page (right now it is rendering across two pages). We are converting to PDF via an HTML string. Code sample below:


string html = RazorToString();

var loadOptions = new HtmlLoadOptions
{
PageInfo = {Margin = new MarginInfo(10, 10, 10, 10)},
CustomLoaderOfExternalResources = CssLoader
};

var doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), loadOptions);
doc.Save();

Hi There,


Thanks for contacting support.

Please use following code snippet to fix your issue

C#

HtmlLoadOptions options = new HtmlLoadOptions(dataDir);
options.PageInfo.Height = 1050;
options.PageInfo.Width = 800;
Document pdfDocument = new Document(dataDir + “test2.html”, options);
pdfDocument.Save(dataDir + “output_generated1.pdf”);

If you still face issues, please share your sample html string to reproduce the issue at our end.

We are sorry for this inconvenience.

Best Regards,

Thank you Fahad, that fit the HTML on a single page. Is there a more robust way that doesn’t involve hardcoding values?


Also, what are the units for the height and width?

When printing a PDF from Acrobat, there are page sizing options that include “Fit”, “Actual Size”, “Shrink oversized pages”, and custom scaling. Is there any way to set those options?

Hi There,


jwp:
Thank you Fahad, that fit the HTML on a single page. Is there a more robust way that doesn’t involve hardcoding values?

Thanks for your feedback. Please note that when you convert HTML into PDF, the API generates a new PDF document in memory, puts the content inside it and then saves it physically by Save() method. During this process, any page width or height parameters which is set in HTML are not being honored. So you need to set the height and width of the page while converting the HTML into PDF like above shared code snippet.

jwp:
Also, what are the units for the height and width?

Please note that the basic measurement unit in
Aspose.Pdf for .NET is point, where 1 inch = 72 points. The values assigned to height and width in the above code snippet, were in points.

jwp:
When printing a PDF from Acrobat, there are page sizing options that include “Fit”, “Actual Size”, “Shrink oversized pages”, and custom scaling. Is there any way to set those options?

I am afraid that currently this feature is not available and have already been logged as PDFJAVA-36684 in our issue tracking system. I have associated the request ID with this thread as well, so that you will be notified once the feature has been added. Please be patient and spare us a little time. We are sorry for the inconvenience.


Best Regards,

That sounds good, thanks for answering all of my questions and associating this thread with the issue log, Asad.

Hi Justin,


As soon as we have some definite updates regarding the resolution of earlier reported issue, we will let you know. Please be patient and spare us little time.
fahadadeel:
Hi There,

Thanks for contacting support.

Please use following code snippet to fix your issue

C#
HtmlLoadOptions options = new HtmlLoadOptions(dataDir);
options.PageInfo.Height = 1050;
options.PageInfo.Width = 800;
Document pdfDocument = new Document(dataDir + "test2.html", options);
pdfDocument.Save(dataDir + "output_generated1.pdf");

If you still face issues, please share your sample html string to reproduce the issue at our end.

We are sorry for this inconvenience.

Best Regards,

This does not work for landscape. I tried this:
options.PageInfo.Height = 1050;
options.PageInfo.Width = 800;

In combination with options.PageInfo.IsLandscape = true and false

Hi Justin,


Thanks for sharing further details.

I have tested the scenario using following code snippet and have not observed any issue. I have attached the generated PDF file for your reference.

C#

HtmlLoadOptions options = new HtmlLoadOptions(dataDir);
options.PageInfo.Width = 1050;
options.PageInfo.Height = 800;

Document pdfDocument = new Document(dataDir + “test.html”, options);
pdfDocument.Save(dataDir + “output_generated.pdf”);

If you still face issue, please share your sample files along with full code to reproduce the issue in our environment.

We are sorry for this inconvenience.

Best Regards,

Is there a way to do the above starting with an existing PDF document that spans multiple pages (as opposed to an html file)?

@albel12146

Thank you for contacting support.

Kindly share requested data in the other thread created by you. We will be assisting you accordingly.