Set print options from JS

So ... from what I can find on Google, I cannot find a way to use Javascript to set my printing preferences (read: margins, orientations, footer, etc.).

I am going to tell my boss that what he wants to do is impossible. I wanted to check if you would be good people to do this. Please let me know.

I found that in FF you can use some user_pref (key, val) parameters to configure user settings, but this does not work for more than 90% of my users. I am looking for something that will cover IE (as much as possible), FF and Chrome.

Thanks to the ninja who answers.

+6
source share
4 answers

So ... after a whole day of looking, I think I found the answer to my question. In short, the answer is that JS and / or CSS will not allow you to override the default page setting for the client browser.

Here is what I have tried. If you create a simple HTML file with an empty head and a body containing only the text β€œTest”, you can see what I tested.

  • Print style sheets cannot solve your problem. Setting a field on the body 0px 0px 0px 0px is not the same as clicking File> Page Setup and setting all of your fields to 0. Although the print style testers would like you to believe, they are different . Try it yourself. This is why Print Style Sheets do not solve your problem.

  • JS cannot solve your problem. Can you create an image if every page you visit has changed your local JS properties? To allow each page to access your local print settings, this will be a security violation and is not allowed. Because of this, JS cannot solve your problem.

I would like someone to answer here and let me know that I am wrong. Otherwise ... it sucks ... and this should happen. I have a ton of old users ... and getting them to set fields in the settings of their page is a pain. Also, the client does not want us to reorganize the page so that they are not needed. I'm between a stone and a hard spot.

+11
source

You can set the page orientation and margin using CSS:

@page { size: landscape; margin: 10% } 

You can also set a footer at the bottom of each page using the <tfoot> element.

EDIT: As Bijamin noted, this is a halfway house. You cannot change the printer settings (those that appear in the Print dialog box when you click Print). In addition, IE is very missing support for the @page selector (looked around to see if IE9 supported it and couldn't find anything). Alternatively, you can always try applying it to the body in your print style sheet. A.

 body { size: landscape; margin: 10% } 

Although I do not know how effective this is.

+6
source

I think you are very unlucky trying to do this in HTML and CSS. Most of the problem is that the printer fields are of the type of printer: most printers have the minimum margin they can install (which is equivalent to the space they need to capture the page and moving their heads left / right).

Valid margins will also vary depending on the paper size (the margin size can usually be smaller on a smaller sheet than the maximum that the printer will accept). For example, if you load A5 paper into an A4 printer, the margins you can set will be different if you load A4.

None of this information is available on a web page, through CSS, javascript, or anything else.

Now, with regard to the solution, PDF files allow you to embed some default printer settings in a file - Acrobat pro allows you to specify the zoom settings, default numbers for copies, etc. I would not mind betting that in a file format that Acrobat does not disclose, there are more potential settings.

There are many fully functional tools for creating PDF files for any server technology used. good ones will even allow you to provide a URL and display HTML + CSS in PDF content - this will somewhat automate the creation. The generation of the PDF code is quite intense, although it will not be smooth for the user (and for most browsers, PDF plugins will be required).

I know this is not perfect, but this is the prospectus that I would study. Good luck

+3
source

Using CSS will help you with this.

 @page { margin:0cm; } 
+1
source

Source: https://habr.com/ru/post/890447/


All Articles