Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtmltopdf print-media-type uses @media print ONLY and ignores the rest

I have an html file that uses one css file. Inside this file at the very bottom i use this for styles that need to be applied ONLY to the printer version of the page

@media print{
   ....print styles here...
}

When I call wkhtmltopdf --print-media-type input.html output.pdf, it renders the pdf with styles that are only in the @media print enclosure and ignores the rest of the styles - which DO NOT have @media type specified

Is this normal, or what am i doing wrong here? Do I need to specify all styles for print inside @media print?

like image 816
Hill Avatar asked Apr 28 '15 05:04

Hill


1 Answers

wkhtmltopdf has an argument --print-media-type you can pass. Here is a C# code example using NReco (for illustrative purposes only), but the parameter should work in exactly the same way:

        var converter = new NReco.PdfGenerator.HtmlToPdfConverter();
        converter.CustomWkHtmlArgs = "--print-media-type";
        var pdfBytes = converter.GeneratePdf(html);
        return pdfBytes;

This works fine for me in C# using NReco to use print media css, and it takes into account any CSS that is not inside a @media block too, such as the font-size of a h3. Try changing the size of the text or something similar and see if the change is reflected.

like image 87
Peter Morris Avatar answered Sep 17 '22 12:09

Peter Morris