parallax / jsPDF

Client-side JavaScript PDF generation for everyone.
https://parall.ax/products/jspdf
MIT License
29.21k stars 4.67k forks source link

printing an iframe with data created from code not is not working #1576

Closed EricG-Personal closed 6 years ago

EricG-Personal commented 6 years ago

I have some sample code located on a branch for jspdf - https://github.com/EricG-Personal/pdf_printing/tree/feature/from_jspdf

I am doing the following to generate the PDF.

var doc = new jsPDF({unit: 'pt', format: 'legal'});
var someText = "hello, world!";
var topCoordinate = 72;
var leftCoordinate = 72;
var padding = 8;

doc.setFont( "helvetica" );
doc.setFontSize( 24 );

var lineHeight      = doc.getLineHeight();
var textWidth       = doc.getTextWidth( someText );
var rectHeight      = ( lineHeight + ( padding * 2 ) );
var halfRectHeight  = rectHeight / 2;
var halfLineHeight  = lineHeight / 2;
var textYCoordinate = topCoordinate + halfRectHeight + halfLineHeight;

doc.setDrawColor( 255, 0, 0 );
doc.rect( leftCoordinate, topCoordinate, textWidth, rectHeight );
doc.text( someText, leftCoordinate + padding, textYCoordinate );

doc.setDrawColor( 0, 0, 0 );
doc.rect( leftCoordinate, textYCoordinate - lineHeight, textWidth, lineHeight );

var blob   = doc.output( 'bloburl' );
var mythis = this;

setTimeout( function() 
{ 
    console.log( "Setting State" );

    mythis.setState({pdfData: blob});
}, 5000);

The PDF data renders correctly in the iframe and I end up calling:

window.frames["pdf_doc"].focus();
window.frames["pdf_doc"].print();

when the onLoad function of the iframe triggers.

However, the page that is to actually be printed is blank.

Uzlopak commented 6 years ago

Seems to me that it is not a jspdf-problem. It is more a Browser-behaviour thing...

By setting the src-attribute of the iframe results in opening the pdf by e.g. Acrobat Reader results in not parsing it by the DOM. Thus results that the print-Method is not working.

https://stackoverflow.com/questions/18835648/print-pdf-from-javascript-embed-tag

Use instead autoprint plugin...

doc.autoPrint();

Uzlopak commented 6 years ago

Closed issue. If you have further questions, we will reopen.