How to import this lib into react typescript?

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

5reactions
gamesovercommented, Oct 25, 2019

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

2reactions
samuelvernoncommented, Jun 14, 2021

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 );
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I use third-party libraries with React and TypeScript?
You can import Javascript libraries in your Typescript project. I do that all the time, unfortunately. Some libraries have community-typed ...
Read more >
Build a component library with React and TypeScript
Learn to create a component library with React and TypeScript in this in-depth tutorial that includes testing and deploying to npm.
Read more >
How to build a React library using TypeScript
A step by step guide to setup a React Library from scratch using TypeScript, and publish it to NPM.
Read more >
TypeScript and React: Getting Started - fettblog.eu
We use React for client-side development. That's why we need to import some libraries to support this. In our case, we import ES...
Read more >
How to create a local React + TypeScript library that I can use ...
1 Answer · Initialize the project · Add TypeScript · Add React · Add a UI component · Add Rollup · Using the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found