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

How to import this lib into react typescript?

Open gamesover opened this issue 5 years ago • 11 comments

I am trying to import this lib into react typescript project.

import html2pdf from 'html2pdf.js'

Then create a typing.d.ts below

declare module 'html2pdf.js';

However, my project reports the below errors

fromRequireContextWithGlobalFallback.js:21 TypeError: _typeof is not a function
    at html2pdf.js:16
    at html2pdf.js:9
    at Object../node_modules/html2pdf.js/dist/html2pdf.js (html2pdf.js:10)

I opened the source file in chrome

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

/**
 * html2pdf.js v0.9.1
 * Copyright (c) 2018 Erik Koopmans
 * Released under the MIT License.
 */
(function (global, factory) {
  (typeof exports === "undefined" ? "undefined" : _typeof(exports)) === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('es6-promise/auto'), require('jspdf'), require('html2canvas')) : typeof define === 'function' && define.amd ? define(['es6-promise/auto', 'jspdf', 'html2canvas'], factory) : global.html2pdf = factory(null, global.jsPDF, global.html2canvas);
})(this, function (auto, jsPDF, html2canvas) {
  'use strict';

  jsPDF = jsPDF && jsPDF.hasOwnProperty('default') ? jsPDF['default'] : jsPDF;
  html2canvas = html2canvas && html2canvas.hasOwnProperty('default') ? html2canvas['default'] : html2canvas;

  var _typeof = typeof Symbol === "function" && _typeof(Symbol.iterator) === "symbol" ? function (obj) {
    return _typeof(obj);
  } : function (obj) {
    return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof(obj);
  };

May I ask what's wrong with my config?

Thank you

gamesover avatar Jul 23 '19 22:07 gamesover

@gamesover Have you fixed it?

muyexi avatar Oct 25 '19 06:10 muyexi

No Luck, I used html2canvas and jsPDF in the end. html2pdf.js seems quite inactive now.

gamesover avatar Oct 25 '19 07:10 gamesover

Anyone?

Samerkassem avatar Sep 05 '20 12:09 Samerkassem

@gamesover Thx dood I was able to follow your trail eons later. To those who come after me : https://stackoverflow.com/questions/26481645/how-to-use-html2canvas-and-jspdf-to-export-to-pdf-in-a-proper-and-simple-way

alexchoiweb avatar Nov 19 '20 07:11 alexchoiweb

For me it works in TypeScript:

import * as html2pdf from 'html2pdf.js';

...

const element = document.querySelector( '.my-element' );
const opt = {
    margin: 1,
    filename: 'myfile.pdf',
    image: { type: 'jpeg', quality: 0.98 },
    html2canvas: { scale: 2 },
    jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
html2pdf.default( element, opt );

samuelvernon avatar Jun 14 '21 14:06 samuelvernon

I can import and print, but the content is blank

rohmansca avatar Feb 11 '22 10:02 rohmansca

I can import and print, but the content is blank

It solved in #517

rohmansca avatar Feb 11 '22 11:02 rohmansca

Has anyone fixed it?

gashiartim avatar Nov 17 '23 00:11 gashiartim

html2canvas

NO

MohdTahirMT avatar Nov 20 '23 11:11 MohdTahirMT

using CDN,

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

then

const newWindowObject = window as any const html2pdf = newWindowObject.html2pdf

annguyenprodev avatar Feb 01 '24 05:02 annguyenprodev