I have a base64 text of a pdf file, that I need to open the print dialog for.
So what I did for now is to create a new window, embedding that into the window for the user to manually click on the "print" at the embed toolbar.
var windowContent = '<!DOCTYPE html>';
windowContent += '<html>'
windowContent += '<head><title>Print</title></head>';
windowContent += '<body style=\"margin:0; padding:0; height:99.5vh\">'
windowContent += '<embed width=100% height=100% src="data:application/pdf;base64,' + dataUrl + '">';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('','','width=1000,height=1000');
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
//printWin.print();
The commented out print
is because the embed loads the pdf async and it tries to print a blank page instead.
I cannot use external JS Libraries like "pdf.js" due to internal security reasons.
What I thought is, that if the embed has that "print" button, if it would be possible to trigger that by code?
Is this even possible?
Edited code due to Comment of Jan Pfeifer: This still does not work properly. The Page is being loaded, but the print is not executing.
function myPrint(win) {
win.print();
}
var windowContent = '<!DOCTYPE html>' +
'<html>' +
'<head><title>Print MeasurementJob</title></head>' +
'<body style="margin:0; padding:0; height:99.5vh">' +
'<embed width="100%" height="100%" src="data:application/pdf;base64,' + dataUrl + '">' +
'</body>' +
'</html>';
var printWin = window.open('', '', 'width=1000,height=1000');
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
var embed = printWin.document.querySelector('embed');
embed.addEventListener('load', function() { myPrint(printWin); });