Posted 4 December 2024, 1:01 pm EST
I am attempting to use PreferCSSPageSize = true while converting some HTML to PDF to allow the CSS to set the width of the PDF page. Here is a simplified code snippet of how I am doing this:
using (var ms = new MemoryStream())
{
PdfOptions options = new PdfOptions() {
PreferCSSPageSize = true
};
htmlPage.SaveAsPdf(ms, options);
return Convert.ToBase64String(ms.ToArray());
}
In the CSS of my HTML I have a page directive that is setting the page width:
<style>
@page {
size: 3.5in 11in;
}
</style>
When I do the conversion this doesn’t appear to be taken into account. Am I doing something wrong? Is there any particular CSS that is required to set the page width when doing the conversion in this manner?
Unfortunately, I can not use the PageWidth option property to set the width. I did hard code the width using PageWidth as a test and this does work, however I need the CSS to determine the page width as I don’t have the width available to the code at the time the HTML is being converted.
Any help would be greatly appreciated.
Thank you,
Bill