How can I open with javascript link data:application/pdf;filename=generated.pdf;base64;DATA
in Chrome 71?
Link from console opened successfully, but not from code - unfortunately.
The snippet does not work for security reason. Only for code demonstration.
I read some similar questions, but did not find an answer.
var button = document.getElementById("button");
button.addEventListener("click", generate, false);
function generate() {
var doc = new jsPDF({
orientation: "l",
unit: "mm"
});
doc.text('ACT', 130, 20);
var string = doc.output('datauristring');
console.log(string);
var link = document.createElement('a');
link.href = string;
link.setAttribute('target', '_blank');
document.body.appendChild(link);
link.click();
link.parentNode.removeChild(link);
}
<script src="https://unpkg.com/jspdf@1.5.3/dist/jspdf.min.js"></script>
<button id="button">Generate pdf table</button>
It's actually very easy, don't complicate things..
or a more verbose and less efficient version:
(You need to open the new window immediately after the onclick or Chrome will block the popup. This solution is not as good because there is an unnecessary conversion from datauri to blob)
You're generating a PDF from its raw data using Javascript. Why not use a reader like Adobe to render it? That is what is happening when you are clicking the link from the console. You could literally just open the link and it will open as a PDF. I think you are possibly over complicating this task.
I run code from the question in the local machine and take this error:
JsPDF - Not allowed to navigate top frame to data URL
I tried the suggestion @WeihuiGuo. Unfortunately for me.
I found than Chrome automaticaly open pdf.
And not only for the new tab. Not opened on the current page too.
https://sphilee.github.io/jsPDF-CustomFonts-support/
Such sad news.
Try
window.open()
instead. The following code worked for me. You will need to modify the window/page size.This code works for me