Angular- ostrzeżenia jsPDF, nieoczekiwane tocken i jsPDF.fromHTML nie są funkcją
Chcę wydrukować kod html z jsPDF w Angular, ale nie mogę. potrzebuję pomocy, spójrz.
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"
]
}
moduł npm jspdf
wprowadź opis obrazu tutaj
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"
]
jeśli w klasie importuję w ten sposób import jsPDF from 'jspdf';
lub w ten sposób, import { jsPDF } from "jspdf";
otrzymuję to ostrzeżenie i ten błąd.
ostrzeżenie:
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
błąd:
scripts.js:18390 Uncaught SyntaxError: Unexpected token 'export'
jeśli importuję jspdf w ten sposób, import * as jsPDF from "jspdf";
otrzymuję ten błąd:
błąd:
Type 'typeof import("jspdf")' has no construct signatures.
const doc = new jsPDF('p', 'pt', 'letter');
to jest moja klasa:
//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);
}
}
a jeśli nie importuję jspdf w angular.json, pojawia się ten błąd, gdy próbuję pobrać:
core.js:4197 ERROR TypeError: doc.fromHTML is not a function
Odpowiedzi
Nie mam dobrego rozwiązania, ale tak to działało. Wygląda na to, że jspdf 2.0.0 zgłasza „doc.fromHTML nie jest funkcją”. w kątowej 10 (nie jestem pewien, czy działa w poprzednich wersjach kątowych).
Usunąłem jsdpf 2.0.0 za pomocą
npm uninstall jspdf
Następnie zainstalowałem poprzednią wersję, która jest:
npm install [email protected]
Przykład pracy:
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');
doc.fromHTML()
Problemem stało się Kątowymi 7.x też. Jeśli downgrade do jsPDF 1.5.3 może działać poprawnie.