eKoopmans / html2pdf.js

Client-side HTML-to-PDF rendering using pure JS.
MIT License
4.15k stars 1.39k forks source link

It's not working on some HTML page #191

Open anil826 opened 5 years ago

anil826 commented 5 years ago

I have an HTML-5 offline page and I am trying to add the CDN script and create a simple PDF but it keeps throwing an exception: Uncaught ReferenceError: html2pdf is not defined

anil826 commented 5 years ago

function addScript(url) { var script = document.createElement('script'); script.type = 'application/javascript'; script.src = url; document.head.appendChild(script); } addScript('https://raw.githack.com/eKoopmans/html2pdf/master/dist/html2pdf.bundle.js');

eKoopmans commented 5 years ago

Hi @anil826, the console "addScript" method doesn't always work, sorry. Some webpages have protection against injecting your own code, which is essentially what that is doing.

There may be a useful error message immediately after you try to add the script, for instance GitHub says "Refused to load the script... because it violates the following Content Security Policy". There may be others who know of workarounds, but I don't.

If you have access to the HTML itself, it's definitely better to just put the script directly into the code, i.e. this method. I'm also working on setting this library up on CDNJS, but you could use that GitHack URL for now if you need it hosted online. Good luck!

bmarsh9 commented 4 years ago

Im getting this error as well and I have loaded the bundled JS directly into the HTML