cefHtmlSnapshot icon indicating copy to clipboard operation
cefHtmlSnapshot copied to clipboard

Adding feature print-media-type

Open ArturGomesSoft opened this issue 4 years ago • 9 comments

Hi. I'm using the wkhtmltopdf tool a while ago now. Because the html that I'm transforming to pdf has styling for screen and print views, i'm using the --print-media-type option to get the printing style, since the need is to have a "printed" version of the document. I am not seeing that option in your help. Is that option lacking or are there any plans to add it in a future version?

Many thanks for the great tool.

ArturGomesSoft avatar Mar 11 '21 09:03 ArturGomesSoft

It's not supported yet, but that's certainly an interesting option to add if possible with CEF.

Just to be sure you're referring to something like this, and being able to choose between print and screen ?

<style>
div {
  border: 1px solid black;
}
@media print {
  div {
    border: 1px dashed black;
    font-size: 300%;
  }
}
</style>
<div>Hello World</div>

EricGrange avatar Mar 11 '21 09:03 EricGrange

Thanks for the reply. Yes... If it would help, I think I can provide you an HTML as an example. The documents I'm converting are medical documentation. The screen media type has some parts that are collapsed and moved from site. The printing part is showing everything and formatted in a popular/official way.

Many thanks for the support.

ArturGomesSoft avatar Mar 11 '21 10:03 ArturGomesSoft

If you have one exemple and it's not confidential, please provide it, it may bring forward issues that do not exist on a more simplistic test case.

I did a a quick full text search on the CEF and web, and could not find an option, though Chrome supports such an option, so it could just be a matter of searching harder.

As a workaround, using the snippet at https://stackoverflow.com/questions/37080744/is-there-a-way-to-enable-media-print-css-to-a-normal-view with the --javascript option may work to force media print or media screen.

Array.prototype.forEach.call(document.getElementsByTagName('style'), function(style) {
    style.innerText = style.innerText.replace(/@media print/gi, '@media screen');
});

It's just a regexp though, so it'll be vulnerable to all kinds of failures... but in a well define context, it could be good enough.

EricGrange avatar Mar 11 '21 17:03 EricGrange

I can provide an example. They are of public domain, in a site that has no login whatsoever.

test1.zip

This file is a typical html file that I should convert to pdf and, currently, using wkhtmltopdf to do the job. If you, from your browser, print the file, you can get the style defined for print media type. That's the style i should use to generate the PDFs.

Many thanks by your efforts.

ArturGomesSoft avatar Mar 11 '21 17:03 ArturGomesSoft

I tested with the html file you provided, and with a pdf output I got the "media print" version while the jpg output resulted in the "media screen" version.

Ar you after getting the "media screen" version for the pdf output, or the "media print" for the jpg version ? (both would be best I guess, but gotta start somwehere)

EricGrange avatar Mar 12 '21 14:03 EricGrange

Hi. I'm always using the "media print", since I have to store a "printed" version of the document. There are several documents similar to that one, but the idea is always the same, i.e., get vision of the thing like if it was printed. I'm not using other types of output.

Therefore, it looks pretty fine like that. When you think you gonna create a definitive version for production?

Many thanks for your time on this.

ArturGomesSoft avatar Mar 12 '21 14:03 ArturGomesSoft

No idea about the definitive version: it is going to chase the CEF PDF support, which as of CEF 89 appears broken in some way :/ Not sure yet if it's somewhere in CEF, Chromium or a wrapper somwhere.

EricGrange avatar Mar 12 '21 15:03 EricGrange

Ok Eric.

Most appreciate the your work. Keep us updated with the news. Thanks a million!

ArturGomesSoft avatar Mar 12 '21 15:03 ArturGomesSoft

Upstream issue https://bitbucket.org/chromiumembedded/cef/issues/3099/cefclient-printtopdf-not-working

EricGrange avatar Mar 15 '21 15:03 EricGrange