@mithunnivas
Summary
Aspose.PDF for .NET does not provide a built‑in “auto‑scale / zoom‑to‑fit” option when converting HTML to PDF. If the HTML contains elements that are wider than the page size defined in the conversion settings, the layout engine will wrap those elements onto the next line or the next page.
Why it happens
When you load HTML with new Document(stream, htmlOptions), the converter creates PDF pages based on the PageInfo supplied in HtmlLoadOptions. The width of the page (in points, 1 inch = 72 points) limits how wide the rendered content can be. HTML itself has no concept of “fit‑to‑page”, so any content that exceeds the page width is broken into a new line or a new page.
What to do next
While there is no single auto‑scale switch, you can try the following approaches:
- Increase the page size (or switch to landscape).
var pageInfo = new PageInfo {
Width = 11 * 72, // 11 inches (or larger as needed)
Height = 8.5 * 72, // 8.5 inches
Margin = new MarginInfo { Top = 36, Bottom = 36, Left = 36, Right = 36 }
};
var htmlOptions = new HtmlLoadOptions { PageInfo = pageInfo };
Adjust the width until side‑by‑side elements fit on a single line.
-
Use FixedLayout = true.
Setting htmlOptions.FixedLayout = true tells the converter to preserve the exact visual layout of the source HTML. If the content is wider than the page, it will be clipped rather than wrapped, which can give a “zoom‑out” effect if clipping is acceptable.
-
Scale the HTML with CSS.
If you cannot change the PDF page size, wrap the whole HTML in a container that applies a CSS transform:
<div style="transform:scale(0.85); transform-origin:top left;">
<!-- original HTML -->
</div>
Tune the scale factor until the rendered PDF fits the desired page dimensions.
-
Combine page‑size increase and CSS scaling for complex layouts.
-
Verify the output after each change to ensure the elements stay side‑by‑side.
Note: The HtmlLoadOptions.PageInfo and FixedLayout properties are available in recent versions of Aspose.PDF for .NET (e.g., 20.10 and later). If you are using an older version, upgrade to a newer build to access these options.
If these work‑arounds do not meet your requirements, you may need to redesign the HTML/CSS so that the content naturally fits within the target page size.
Disclaimer: The suggestions above are based on typical usage of Aspose.PDF and are not directly quoted from official documentation in the provided evidence. Please test them in your environment and refer to the official Aspose.PDF for .NET API reference for exact syntax and version compatibility.
I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.
SDK version unclear — refer to the official release notes before applying this guidance.