I got a new PDF generator using just javascript and no server codes implemented. But when I always try to run the code the file always automatically save or download so there are no chance for me to review or view the content. How can I view the content of my PDF when I am usingg jsPDF?
var name = prompt('What is your name?');
var multiplier = prompt('Enter a number:');
multiplier = parseInt(multiplier);
var doc = new jsPDF();
doc.setFontSize(22);
doc.text(20, 20, 'Questions');
doc.setFontSize(16);
doc.text(20, 30, 'This belongs to: ' + name);
for(var i = 1; i <= 12; i ++) {
doc.text(20, 30 + (i * 10), i + ' x ' + multiplier + ' = ___');
}
doc.addPage();
doc.setFontSize(22);
doc.text(20, 20, 'Answers');
doc.setFontSize(16);
for(var i = 1; i <= 12; i ++) {
doc.text(20, 30 + (i * 10), i + ' x ' + multiplier + ' = ' + (i * multiplier));
}
doc.save('Test.pdf');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js"></script>
window.open(doc.output('bloburl'), '_blank');
Although window.open
works fine, for security reasons new chrome versions are blocking by default the popup and hence I've needed another solution.
Inspired by the page of: https://parall.ax/products/jspdf I've concluded to the below solution:
HTML code (for full size iframe on the screen):
<iframe id="main-iframe" style="width: 100%; height: 100%; position: fixed; top: 0; left: 0; z-index: 2; border: none;"></iframe>
JavaScript code:
document.getElementById('main-iframe').setAttribute('src', doc.output('bloburl'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With