简体   繁体   中英

window.print() not applying the css while converting html to pdf

I am facing issue in print the pdf from html, I am using window.print() function but it is not applying the CSS, please someone help me into this issue

This is my code, here I am including CSS files at 3rd and 4th line but that does not working

CSS are in two to three files it is external css not inline, I tried adding @media print in those file but does not worked, let me know if i referencing those css files correctly or not in my below code, it is reactjs application

let mywindow = window.open('', 'PRINT', 'height=650,width=900,top=100,left=150');

mywindow.document.write(`<html><head><title>test</title>`);
mywindow.document.write( `<link rel="stylesheet" href="style.css" type="text/css" media="print"/>` );
mywindow.document.write( `<link rel="stylesheet" href="App.css" type="text/css" media="print"/>` );
mywindow.document.write(`</head><body>`);

mywindow.document.write(document.getElementById("owtab").innerHTML);
mywindow.document.write(`</body></html>`);

mywindow.document.close();
mywindow.focus();

mywindow.print();
mywindow.close();

You need to use the media query for print inorder to provide css when printing. Check this Link .

You can use as follows:

@media print {
    /* styles here */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM