Hello,
I am trying to use this library in my Typescript application. But seems like we do not have a type definition file for this library in Definitely Typed repo. Is there any plans to generate one?
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1 really needed
+1
+1
+1
Any news on this front?
+1
+1
+1
Still nothing?
+1
+1
+1
+1
+1
+1 for Typescript definitions.
+1
+1
+1
I see that @types/pdfmake exists now, but I don't understand how to initialize PDFMake and VFS with those type definitions. I tried this, which seems to be the closest to my previous code:
import {pdfMake} from "pdfmake/build/pdfmake";
import {pdfMake as pdfFonts} from "pdfmake/build/vfs_fonts";
...
pdfMake.vfs = pdfFonts.vfs;
And that gives me an error that pdfMake is undefined (undefined is not an object for pdfMake.vfs).
The only working code I have is this, which doesn't look very attractive:
import * as pdfMake from "pdfmake/build/pdfmake";
import * as pdfFonts from 'pdfmake/build/vfs_fonts';
...
(<any>pdfMake).vfs = pdfFonts.pdfMake.vfs;
How did you add pdfmake to @types ? can you please help
If you have @types/pdfmake in your package.json and run "npm install" types should show up automatically in your IDE. Otherwise your tsconfig.json most likely has incorrect "typeRoots" property (or other IDE setup issue)
I tried but it seams, it's old or incorrect!
can anybody take a look at that please?
based on the test at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/pdfmake/pdfmake-tests.ts
this worked for me
import * as pdfMake from 'pdfmake/build/pdfmake';
import * as pdfFonts from 'pdfmake/build/vfs_fonts';
const pdf = pdfMake;
pdf.vfs = pdfFonts.pdfMake.vfs;
Above type definition file seems incomplete at the moment. The following use case (server-side) throws typescript error "Cannot use 'new' with an expression that lacks a call or constructor signature" on the 1st line:
const printer = new pdfMake(fonts)
const doc = printer.createPdfKitDocument(docDefinition)
doc.pipe(fs.createWriteStream('output.pdf'))
doc.end()
Similar code works OK in plain javascript.
Any comments/plans to include?
Has anyone tried using pageOrientation when using PDFMake in TypeScript?
I got an error that neither pageOrientation or pageSize is part of the documentDefinition. But it is working if I am using it as part of a text object inside the content brackets. Like this (copied from ReadMe):
pageOrientation: 'portrait',
content: [
{text: 'Text on Portrait'},
{text: 'Text on Landscape', pageOrientation: 'landscape', pageBreak: 'before'},
{text: 'Text on Landscape 2', pageOrientation: 'portrait', pageBreak: 'after'},
{text: 'Text on Portrait 2'},
]
I'm providing a gist that you could include in your project: https://gist.github.com/manu-st/fcf925e389f352162a931a1089e01918. It may contain errors and might be incomplete. I'll update it in the next couple of days after working more with the pdfMake library to create my PDFs.
@manu-st How did you use that interface exactly?
Specifically, it seems to be missing imports like "PDFKit", "IPdfPrinter"
Yes. I want types for TypeScript.
pdfmake is the best PDF JS tool in the planet.
Running into errors especially when trying to set pageSize, pageOrientation and pageMargins... all return errors:
PageSize:
ERROR in src/app/export-order/export-order.component.ts(18,13): error TS2540: Cannot assign to 'vfs' because it is a read-only property.
src/app/export-order/export-order.component.ts(136,23): error TS2345: Argument of type '{ pageSize: string; content: { text: string; }; }' is not assignable to parameter of type 'TDocumentDefinitions'.
Types of property 'pageSize' are incompatible.
Type 'string' is not assignable to type 'PageSize'.
PageOrientation:
ERROR in src/app/export-order/export-order.component.ts(18,13): error TS2540: Cannot assign to 'vfs' because it is a read-only property.
src/app/export-order/export-order.component.ts(139,23): error TS2345: Argument of type '{ pageSize: string; pageOrientation: string; content: { text: string; }; }' is not assignable to parameter of type 'TDocumentDefinitions'.
Types of property 'pageOrientation' are incompatible.
Type 'string' is not assignable to type 'PageOrientation'.
PageMargins:
ERROR in src/app/export-order/export-order.component.ts(18,13): error TS2540: Cannot assign to 'vfs' because it is a read-only property.
src/app/export-order/export-order.component.ts(141,23): error TS2345: Argument of type '{ pageSize: string; pageOrientation: string; pageMargins: number[]; content: { text: string; }; }' is not assignable to parameter of type 'TDocumentDefinitions'.
Types of property 'pageMargins' are incompatible.
Type 'number[]' is not assignable to type 'Margins'.
Type 'number[]' is missing the following properties from type '[number, number, number, number]': 0, 1, 2, 3
I assume this is a typing error or some version incompatability as I copied the settings directly from the docs on pdfmake? In the playground it seems works (tested the page size)...
Environment and versions are:
Angular CLI: 8.0.3
Node: 12.4.0
OS: win32 x64
Angular: 8.0.1
for pageSize you can use the PageSize enum (e.g. instead of 'A4' use PageSize.A4)
or ('A4' as PageSize)
Veo que
@types/pdfmakeexiste ahora, pero no entiendo c贸mo inicializar PDFMake y VFS con esas definiciones de tipo. Intent茅 esto, que parece ser el m谩s cercano a mi c贸digo anterior:import {pdfMake} from "pdfmake/build/pdfmake"; import {pdfMake as pdfFonts} from "pdfmake/build/vfs_fonts"; ... pdfMake.vfs = pdfFonts.vfs;Y eso me da un error de que pdfMake no est谩 definido (undefined no es un objeto para pdfMake.vfs).
El 煤nico c贸digo de trabajo que tengo es este, que no se ve muy atractivo:
import * as pdfMake from "pdfmake/build/pdfmake"; import * as pdfFonts from 'pdfmake/build/vfs_fonts'; ... (<any>pdfMake).vfs = pdfFonts.pdfMake.vfs;
import { createPdf, TDocumentDefinitions } from "pdfmake/build/pdfmake";
import * as v from "pdfmake/build/vfs_fonts";
let pdf: TDocumentDefinitions = {
pageSize: 'A2',
header: {
columns: [
{ text: 'Miau!' }
]
},
content: [
]
}
createPdf(pdf, null, {
Roboto: {
normal: 'Roboto-Regular.ttf',
bold: 'Roboto-Medium.ttf',
italics: 'Roboto-Italic.ttf',
bolditalics: 'Roboto-MediumItalic.ttf'
},
Courier: {
normal: 'Courier',
bold: 'Courier-Bold',
italics: 'Courier-Oblique',
bolditalics: 'Courier-BoldOblique'
},
}, v.pdfMake.vfs).download('pdfname.pdf');
@dgzraul unfortunately that is due to the way that this library exposes functionality, it's not built for ES6 (or by association, TS) import statements. If you're looking for a slightly cleaner way to import and use PDFMake inside a TS application, then perhaps pdfmake-wrapper might interest you.
from Google Translate:
desafortunadamente eso se debe a la forma en que esta biblioteca expone la funcionalidad, no est谩 construida para las declaraciones de importaci贸n ES6 (o por asociaci贸n, TS). Si est谩 buscando una forma un poco m谩s limpia de importar y usar PDFMake dentro de una aplicaci贸n TS, entonces tal vez
pdfmake-wrapperpueda interesarle.
If you are here because you installed @types/pdfmake and it's not working for you, you may need to add types to your tsconfig:
...
"types": ["pdfmake"]
...
Thanks to this thread for helping me out: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/42761
For future you searching for help, my error was:
"Could not find a declaration file for module 'pdfmake/build/pdfmake'."
I'm not sure if anyone is still having issues with this, but I was able to get this working with TypeScript by taking the specific things I needed from @types/pdfmake. For example, to use a StyleDictionary:
import { StyleDictionary } from 'pdfmake/interfaces';
const styles: StyleDictionary = {
...
};
The same goes for Content, Column, etc.
Most helpful comment
based on the test at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/pdfmake/pdfmake-tests.ts
this worked for me
import * as pdfMake from 'pdfmake/build/pdfmake';
import * as pdfFonts from 'pdfmake/build/vfs_fonts';
const pdf = pdfMake;
pdf.vfs = pdfFonts.pdfMake.vfs;