edabg / jsprintsetup

JSPrintSetup Firefox addon
Mozilla Public License 2.0
76 stars 39 forks source link

Using this plugin to print specific DOM elements #19

Open bogal opened 7 years ago

bogal commented 7 years ago

Is it possible to use this plugin to print specific elements? For instance, i have an array of images (shipping labels) that need to be printed - is it possible to send each image to JsPrintSetup to print silently?

mitkola commented 7 years ago

jsPrintSetup.printWindow can be used to print IFrame. If you can transform list of images as IFrames you can print separately.

bogal commented 7 years ago

Thanks for the quick reply. But can you clarify what you mean by transforming a list of images into an iframe? I'm assuming this is something i can do via the dom, instead of having to rewrite the page to have each inside an iframe? Will it only print an iframe or window, or is is possible to create a new document and send that to be printed?

What i am trying to achieve is a list of images, with a print button below each one. When the button is clicked, it silently sends the image to jsPrintSetup to be printed (one image per printed page).

bogal commented 7 years ago

I think this is along the lines of what you mean, but the iframe isn't being printed:

//imageSrc = url of image to be printed
var myFrame = $('<iframe>');
myFrame.attr('src', imageSrc);
jsPrintSetup.printWindow(myFrame);
jsPrintSetup.setOption('printSilent', 0);
mitkola commented 7 years ago

You must pass window object to jsPrintSetup.printWindow method. $('<iframe>') I think not do so. The simplest way is ti try jsPrintSetup.printWindow(window.frames[0]) to print content of first frame. Refer to https://developer.mozilla.org/en-US/docs/Web/API/Window/frames

bogal commented 7 years ago

Yes, that will print if i call jsPrintSetup.printWindow(window.frames[0]).

However, with multiple images in a page, is it possible to have a single (hidden) i frame, then just change the src of each one to the src of the image that i want to print?

eg:


//dom contains a single iframe with src= "" style = "visibility:hidden;"
//imgSrc is url of image to print
windows.frames[0].src = imgSrc;
jsPrintSetup(windows.frames[0])

...so that a single iframe element in the dom is being used as a container to print different images.

bogal commented 7 years ago

... as it appears that i cannot set the src of the frame property dynamically. Even this is not working:

  var frame = window.frames[0];
   frame.src = imgSrc;
jsPrintSetup(frame)

...will post the contents of windows.frames[0], regardless of whether i change the src to this or not.

mitkola commented 7 years ago

jsPrintSetup.printWindow(frame) Also you must wait frame to be loaded!

hidden4003 commented 6 years ago

If you have several iframes on page and want to print the needed one by using DOM element or id then in mozilla you can do it as follows:

var myFrame = $('<iframe>');
myFrame.on('load', function(){
jsPrintSetup.printWindow(myFrame.contentWindow);
jsPrintSetup.setOption('printSilent', 0);
});
myFrame.attr('src', imageSrc);