Open jsPDF created pdf in Chrome's new tab/window

前端 未结 5 692
我寻月下人不归
我寻月下人不归 2021-01-03 16:01

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 fro

5条回答
  •  爱一瞬间的悲伤
    2021-01-03 16:37

    It's actually very easy, don't complicate things..

    window.open(URL.createObjectURL(doc.output("blob")))
    

    or a more verbose and less efficient version:

    let newWindow = window.open('/');
    fetch(doc.output('datauristring')).then(res => res.blob()).then(blob => {
        newWindow.location = URL.createObjectURL(blob);
    })
    

    (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)

提交回复
热议问题