Trying to use a client side pdf book PDF file in my Angular 2 project (version = 4.2.x).
In the file .angular-cli.json, I declared js as follows:
"scripts": [
"../node_modules/pdfmake/build/pdfmake.js",
"../node_modules/pdfmake/build/vfs_fonts.js"
]
And then in app.component.ts, I used it like this:
import * as pdfMake from 'pdfmake';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
pdf: any;
downloadPdf() {
let item = {firstName: 'Peter', lastName: 'Parker'};
this.pdf = pdfMake;
this.pdf.createPdf(buildPdf(item)).open();
}
}
function buildPdf(value) {
var pdfContent = value;
var docDefinition = {
content: [{
text: 'My name is: ' + pdfContent.firstName + ' ' + pdfContent.lastName + '.'
}]
}
console.log(pdfContent);
return docDefinition;
}
When loading the application, I encountered an error in the browser console:
Uncaught TypeError: fs.readFileSync is not a function
at Object.<anonymous> (linebreaker.js:15)
at Object.<anonymous> (linebreaker.js:161)
at Object.../../../../linebreak/src/linebreaker.js (linebreaker.js:161)
at __webpack_require__ (bootstrap b937441…:54)
at Object.../../../../pdfmake/src/textTools.js (textTools.js:4)
at __webpack_require__ (bootstrap b937441…:54)
at Object.../../../../pdfmake/src/docMeasure.js (docMeasure.js:4)
at __webpack_require__ (bootstrap b937441…:54)
at Object.../../../../pdfmake/src/layoutBuilder.js (layoutBuilder.js:7)
at __webpack_require__ (bootstrap b937441…:54)
My workaround to solve this problem:
Copy pdfmake.js and vfs_fonts.js to the resources folder, and then add this to index.html:
<script src='assets/pdfmake.min.js'></script>
<script src='assets/vfs_fonts.js'></script>
Remove this from app.component.ts
import * as pdfMake from 'pdfmake';
And add this to app.component.ts:
declare var pdfMake: any;
Finally, remove this from .angular-cli.js:
"../node_modules/pdfmake/build/pdfmake.js",
"../node_modules/pdfmake/build/vfs_fonts.js"
It works, but it is still a workaround.
Does anyone know how to use this library in Angular / Typscript mode?
Many thanks!