1

I need to print a Secret(Security) page contain "username" and "password" of user login information with JavaScript on the browser.

I do not want the contents of this page to be visible for user creator, When user click on Print button, Just i want to send data to printer as hidden, I mean user just see username and password values on A4 paper and no on the print preview on browser.

Thanks

Sajjad Dehghani
  • 640
  • 1
  • 6
  • 15
  • 1
    I don't believe you can change preview with javascript in browser. Preview should be exactly the same as final result, so I know what I'm printing. – apple apple Jan 01 '19 at 07:55
  • 1
    and what's the reason to do this? it just make no sense to me. – apple apple Jan 01 '19 at 07:58
  • 1
    Just so you know, hidden elements are still visible in the source of the html page. The `hidden` value just tells the browser not to render the elements. They are therefore not very secret. – Joseph_J Jan 01 '19 at 07:58
  • @apple-apple I have a admin panel and a admin user that can create user accounts for users, I dont want to admin user see username or password as plain text on print page before print it. – Sajjad Dehghani Jan 01 '19 at 08:01
  • 1
    You can write a css script specifically for printing. You can create a css class for elements that are hidden and change them so they are not. – Joseph_J Jan 01 '19 at 08:02
  • @joseph-j Yeah, I know, But how to send secret data to printer that admin user can not see values, Password is secret and i dont want to admin can see it, Just i want ot can print it and can not see on preview page – Sajjad Dehghani Jan 01 '19 at 08:03
  • 1
    I believe disabling the "Print Preview" would be a browser specific setting, i.e. a CLI flag set to disable print preview. I've done this for kiosk software I've designed. There's just no way to do it from JS within a browser due to the nature of the way browsers sandbox your code. – Drew Reese Jan 01 '19 at 08:03
  • @joseph-j Of course, But how to hidden element but print it? – Sajjad Dehghani Jan 01 '19 at 08:05
  • 1
    You can make whatever behavior you want - hide, don't hide ect. – Joseph_J Jan 01 '19 at 08:07
  • 1
    Read this: Should give you an idea. https://stackoverflow.com/questions/32699436/media-print-css – Joseph_J Jan 01 '19 at 08:08
  • 1
    @SajjadDehghani since there exist many virtual printer (pdf printer, image printer...), I don't think it would work in any way. – apple apple Jan 01 '19 at 08:08

1 Answers1

1

Best you can do in-browser without being able to disable a Print Preview you can achieve it via CSS media queries, the @media screen should be hidden, and @media print should not.

Drew Reese
  • 165,259
  • 14
  • 153
  • 181