JuanDa237
JuanDa237

Reputation: 376

Angular- jsPDF warnings, uncexpected tocken and jsPDF.fromHTML is not a function

I want to print html code whit jsPDF in Angular but i cant. i need help, look.

ts.config.app.json

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [],
    "allowSyntheticDefaultImports": true
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

npm jspdf module

enter image description here

angular.json scripts

 "scripts": [
              "node_modules/jquery/dist/jquery.js",
              "node_modules/popper.js/dist/umd/popper.js",
              "node_modules/bootstrap/dist/js/bootstrap.js",
              "node_modules/jspdf/dist/jspdf.es.min.js"
            ]

if in the class i import like this import jsPDF from 'jspdf'; or like this import { jsPDF } from "jspdf"; i get this warning and this error.

warning:

WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js 195:141-151
Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js 195:240-254
Critical dependency: the request of a dependency is an expression

error:

scripts.js:18390 Uncaught SyntaxError: Unexpected token 'export'

if i import jspdf like this import * as jsPDF from "jspdf"; i get this error:

error:

Type 'typeof import("jspdf")' has no construct signatures.
    const doc = new jsPDF('p', 'pt', 'letter');

this is my class:

//PDF
// import { jsPDF } from "jspdf";
import jsPDF from 'jspdf';
// import * as jsPDF from "jspdf";

@Component({
  selector: 'app-cash-register',
  templateUrl: './cash-register.component.html'
})
export class CashRegisterComponent implements OnInit {

@ViewChild("ticket") ticket: ElementRef;

public printPDF(): void {
    var pdf = this.ticket.nativeElement.innerHTML;
    const doc = new jsPDF('p', 'pt', 'letter');

    const margins = {
      top: 80,
      bottom: 60,
      left: 40,
      width: 522
    };

    doc.html(pdf, {
      callback: function(doc) {
        doc.save("Test1.pdf");
      }
    })

    // or
    
    doc.fromHTML(pdf, margins.left, margins.top, {}, function () {

      doc.save("Test2.pdf");
    }, margins);
  }
}

and if i not import jspdf in angular.json i get this error when i tried to download:

core.js:4197 ERROR TypeError: doc.fromHTML is not a function

Upvotes: 4

Views: 5516

Answers (2)

Nakres
Nakres

Reputation: 1602

I don't have a good solution but this is how I made it work. It looks like jspdf 2.0.0 is throwing "doc.fromHTML is not a function." in angular 10 (I am not sure if it works in the previous angular versions.)

I removed jsdpf 2.0.0 using

npm uninstall jspdf

Then I installed the previous version which is:

npm install [email protected]

WorkingExample:

import * as jsPDF from 'jspdf';


var doc = new jsPDF('p', 'pt', 'letter');
var imgData =
  'data:image/png;base64,addAnImageData';
var specialElementHandlers = {
  '#idDiv': function (element, renderer) {
    return true;
  }
};
doc.addImage(imgData, 'png', 0, 250, 615, 200);
let source = document.getElementById("idDiv");
doc.fromHTML(source, 0, 0, {
  'elementHandlers': specialElementHandlers
});
doc.save('card.pdf');

Upvotes: 6

hyzhang6639
hyzhang6639

Reputation: 1

The doc.fromHTML() issue happened on Angular 7.x too. If downgrade to jsPDF 1.5.3, it can work properly.

Upvotes: 0

Related Questions