html2pdf.js icon indicating copy to clipboard operation
html2pdf.js copied to clipboard

Does this work with Vite?

Open jordcodes opened this issue 2 years ago • 6 comments

Just trying to get this library working in a vue 3 project which uses Vite and not webpack.

Just passing in a simple html div into html2Pdf and see following error:

TypeError: html2canvas__WEBPACK_IMPORTED_MODULE_9__ is not a function at Promise2.toCanvas_main (http://localhost:8081/node_modules/.vite/html2pdf_js.js?v=95ddde7e:9804:24) at invokeCallback (http://localhost:8081/node_modules/.vite/html2pdf_js.js?v=95ddde7e:12490:29) at publish (http://localhost:8081/node_modules/.vite/html2pdf_js.js?v=95ddde7e:12479:21) at MutationObserver.flush (http://localhost:8081/node_modules/.vite/html2pdf_js.js?v=95ddde7e:12293:19)

Anyone got this package working with Vite?

jordcodes avatar May 23 '22 16:05 jordcodes

I have the same problem

icefee avatar May 27 '22 07:05 icefee

貌似是html2canvas的版本问题,尝试修改src/worker.js 第二行 import * as html2canvas from 'html2canvas'; 改成 import html2canvas from 'html2canvas'; 重新编译生成

icefee avatar May 27 '22 07:05 icefee

having the same problem, did you guys solved it?

alttabs avatar Nov 09 '22 21:11 alttabs

Hey, @alttabs @icefee @jordcodes

I have a solution.

Add below code to index.html:

 <script
      src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"
      integrity="sha512-GsLlZN/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA/HdeKFVgA5v3S/cIrLF7QnIg=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    ></script>

you need to suppress typescript error for the library with @ts-ignore like below:

// @ts-ignore
html2pdf()

you don't need import anything, html2pdf is global now

ridvanaltun avatar Jul 20 '23 12:07 ridvanaltun

@ridvanaltun So we can't use the npm version?

risalfajar avatar Jan 31 '24 03:01 risalfajar