Using PreferCSSPageSize during HTML conversion

Posted by: bill.emerson on 4 December 2024, 1:01 pm EST

    • Post Options:
    • Link

    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

  • Posted 5 December 2024, 4:34 am EST

    Hi Bill,

    We are not able to reproduce the issue you mentioned on our end using DsHtml Version 7.2.5. Please refer to the attached GIF which illustrates that the PDF is generated with a page width of 3.5 inches and a page height of 11 inches as specified in the CSS of the HTML - HtmlToPdf.zip

    You can also refer to the attached sample that we created on our end to reproduce the mentioned issue - CSSPageSize.zip

    In case you are using a previous version of DsHtml, we recommend you upgrade to the latest version, and if the issue is replicable on your end in the latest version too, please share the following details with us:

    1. A stripped-down sample replicating the issue.
    2. Machine configurations.

    Best Regards,

    Kartik

  • Posted 6 December 2024, 12:30 pm EST

    Thank you for the code sample. I had a typo on my side that was causing the issue. Appreciate the quick response!

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels