简体   繁体   中英

How to set Html Page Width and Height for print using Javascript

I am trying to design a webpage that needs to be printed/saved only at certain height and width when printing (or) saving it as pdf using "Microsoft Print to PDF".

I tried

 @media print{
@page{
margin-top; 2px;
margin-botton" 2px;
size: 5in 7in;
}

and a lot of similar variations but no matter what I do the exported pdf always is in 8.5*11 size.

How do I print a webpage by setting the page size and margins to my specification. This should work in IE11

Maybe it's because you typed your message really quickly, but the code you are using is not valid css...

Regardless, this is from MDN:

You can only change the margins, orphans, widows, and page breaks of the document. Attempts to change any other CSS properties will be ignored.

This makes sense if you think about it, as you can't be sure what paper size a user will be using.

So what I'd suggest is applying the height and width to an element in your html, maybe add a black border to it that can be used to cut out your document from the resulting print out.

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