Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printing needs more options #7849

Closed
pablooneto opened this issue Nov 2, 2016 · 30 comments · Fixed by #18984
Closed

Printing needs more options #7849

pablooneto opened this issue Nov 2, 2016 · 30 comments · Fixed by #18984

Comments

@pablooneto
Copy link

  • Electron version: 1.4.5
  • Operating system: All

1- There are only 2 options when it comes to print: silent and printBackground, there are plenty more options, like margins, copies, etc in the print window (Ctrl+P), that could be sent using webContents.print()

2- Access to print queue, to know if the printer is printing

@baconbrad
Copy link
Contributor

You can have a lot of control of the document with CSS using @page.

For copies you could just loop or repeat the print function to send it as multiple jobs.

@zcbenz
Copy link
Member

zcbenz commented Nov 16, 2016

To summary, the things we need to add for better printing support:

  • New options:
    • Select printer for printing
    • Header and footer
    • Copies
    • Other things from the print dialog
  • New APIs:
    • List available printers
    • Access to print queue

@pablooneto
Copy link
Author

A callback when printing is done could be nice, but I think this are the minimum options necessary to develop something that actually need to print stuff.
About the margins, I was talking about Chromium printing options, not content margins

@startupfoundry
Copy link

Silent printing also doesn't let you select a printer. It just goes to the default printer (as seen here) --> #5572

@zeke
Copy link
Contributor

zeke commented Nov 18, 2016

The electron-printer package might be helpful.

@renaesop
Copy link
Contributor

renaesop commented Jan 19, 2017

#8596 maybe helps

@SinghSatyam
Copy link

How do I pass custom margin parameter for marginsType: 4?

@destan
Copy link
Contributor

destan commented May 31, 2017

How do I pass custom margin parameter for marginsType: 4?

currently you can't. see:

@timfish
Copy link
Contributor

timfish commented Jun 27, 2017

@zcbenz in electron@1.6.11, @page {size: landscape} does not force printing into landscape orientation. It would be good if the options included landscape: boolean like in PDF generation.

Perhaps if electron had an API for printing PDF's and then webContents.print could simply call webContents.printToPdf and pass the result to the PDF printer? Then the print/printToPdf options could all be the same and only 1 API would have to be maintained/tested? This would then support print-preview using PDF's and printing that output would be simpler.

@DTeuchert
Copy link

DTeuchert commented Aug 28, 2017

Do you have any release plans for integrating these options in webContents.print (#9868):

  • marginsType: 0,
  • landscape: true

It will be nice to use these options instead of solving it via css
@media print { @page {size: landscape} }

@jasonfutch
Copy link

jasonfutch commented Nov 9, 2017

Need a way to pass data directly to printer for RAW printing to thermal and label printers. The same functionality available in node-print call printDirect()

@renaesop
Copy link
Contributor

@jasonfutch you can use node-print instead.

@faviovelez
Copy link

faviovelez commented Nov 18, 2017

Is it possible to print HTML documents with electron?

A friend and I are developing a POS desktop app and need to print tickets with a generic (text only) printer. We generete tickets from dynamic HTML and use: win.loadURL("data:text/html;charset=utf-8," + encodeURI(html)) to read it and then win.webContents.print({silent: true) to print it. It works well on a normal printer but with a generic printer it's just white paper.

Printing HTML pages from browser works OK but not when using Electron. We are testing on Windows 10 x64, Electron 1.7.9.

@alex-griffiths
Copy link

@renaesop is there anyway at all to pass data directly to the printer in electron? I've been trying for hours to get node-printer to work on my Windows machine, and I'm having absolutely no luck.

@Hyperblaster
Copy link

I really need the option to provide a page range, EG only print page 1 when using webContents.printToPDF()

@hicaro
Copy link

hicaro commented Jul 19, 2018

Is there a way to detect when the user cancelled the job, after calling window.webContent.print?
What I mean is, the print dialog comes up, and then user does not click on Print but rather clicks on Cancel.

This is what I have so far:

const printOptions = {
  silent: false,
  printBackground: true,
  deviceName: ''
};

window.webContents.print(printOptions, (success: boolean) => {
   if (!success) {
     // deal with the error
   }
 });

In the situation I described above, the callback is never triggered.

@abhijithcs
Copy link

@faviovelez Were you able to find any solution?

@baconbrad
Copy link
Contributor

@abhijithcs @faviovelez In our POS we have an element (#printer-content) with visibility: hidden; as part of the style. And then with CSS settings we can dial it in for the printer we are using. Now you can take HTML content and put them in #printer-content. (Be sure to use promises to ensure the data is ready.) It works both in Electron and a web browser. Since we print multiple documents at a time such as a customer and merchant receipt we had to write a print queue system. But you implement that in a wide range of ways.

Depending on the type of printer we load in a style sheet with the best settings for it. Because unfortunately CSS for one printer won't work for another completely different type.

Here is one for a regular A4 printer:

@media print {
  @page {
    size: A4;
  }
  body * {
    visibility: hidden;
  }
  #printer-content, #printer-content * {
    visibility: visible;
  }
  #printer-content {
    position: absolute;
    left: 0;
    top: 0;
  }
}

Or a Star receipt printer:

@media print {
  @page {
    margin: 0;
  }
  body * {
    visibility: hidden;
  }
  #printer-content, #printer-content * {
    visibility: visible;
  }
  #printer-content {
    position: absolute;
    left: 0;
    top: 0;
    width: 71mm
    min-width: 71mm;
    max-width: 71mm;
    font-size: 12px;
    font-family: "Courier New", Courier !IMPORTANT;
    font-weight: 700;
  }
}

@aknuds1
Copy link

aknuds1 commented Sep 11, 2018

I'm wondering about the same as @hicaro does, i.e. when the user cancels the print dialog there's no callback. This is troublesome, as I would like to know when the print dialog closes.

@JorgeHawkins
Copy link

JorgeHawkins commented Oct 3, 2018

Repeating the printing function does not work for tag printers (like Zebra printers). The printer expects either the quantity of tags to print them all at once, or a cooldown between different tasks. Repeating the printing task actually makes each repetition a different printing task, thus requiring a timeout for each one.

@seriouz
Copy link

seriouz commented Oct 3, 2018

@JorgeHawkins Yes but this is a problem with the printing drivers.

@JorgeHawkins
Copy link

@JorgeHawkins Yes but this is a problem with the printing drivers.

No, it isn't. Why can a native app print many tags properly through these kind of printers, but Electron apps have to repeat the process one by one? Makes no sense to make the driver the culprit over an incomplete implementation of a functionality.

@seriouz
Copy link

seriouz commented Oct 5, 2018

@JorgeHawkins i still don't understand fully what you are talking about exactly. Can your print a normal website with the normal chrome browser print dialogue many times on your zebra without any issues?

@baconbrad
Copy link
Contributor

baconbrad commented Oct 5, 2018

@JorgeHawkins I think what you are asking is already in Chrome. Could you introduce page breaks to solve this? This would allow you to send one job but have it print on separate pages, or in this case tags.

@Hyperblaster
Copy link

Hyperblaster commented Oct 6, 2018

If it helps, our workaround for the issue was to printToPDF multiple pages, the merge them together as a single PDF (it's pretty fast)

and then print the merged PDF file from the command line using CUPS on Mac, and an external EXE File on Windows.

We're printing on Brother and Dymo label printers. But found the same massive delay if we send multiple print jobs to the printer as opposed to a single print job with multiple pages.

@csga5000
Copy link

@zcbenz Chromium supports headers and footers via some extra arguments. They can be used in puppeteer (googles headless node package for chrome):

const puppeteer = require('puppeteer');

(async() => {
	const browser = await puppeteer.launch();
	const page = await browser.newPage();
	await page.goto('file:///C:/path/page.html');
	await page.pdf({
		path: 'pageoutput.pdf',
		format: 'A4',
		displayHeaderFooter: true,
		headerTemplate: '<div></div>',
		footerTemplate: 
			'<div style="font-size: 12px; width: 100%; text-align: center; font-style: italic; padding-bottom: 0.5in; font-family: serif;">'+
				'Page <span class="pageNumber"></span>'+
			'</div>',
		margin: {
			top: '0.5in',
			bottom: '1.5in',
			left: '1in',
			right: '1in',
		}
	});

	await browser.close();
})();

@selabie68
Copy link

Is there any support or plan of support for the "Scale" function?

@philipparndt
Copy link

philipparndt commented Dec 16, 2018

@selabie68 you can achieve scaling by using css:

@media print {
    html, body {
        zoom: 150%;
    }
}
webContents.insertCSS('@media print {html, body {zoom: 150%;}');

nevertheless, a setting would be great.

@selabie68
Copy link

Thanks @philipparndt

I will test this method. Does it have the same effect as the Chromium scale option?

screenshot from 2018-12-17 09-01-09

@bhaktigohel
Copy link

bhaktigohel commented Mar 25, 2019

hello @zcbenz ,

can you please let me know if I can print multiple copies without looping for print counts?
Is there any option for set how much copies I want to print?

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.