Home > front end >  How to use html-pdf-node in typescript
How to use html-pdf-node in typescript

Time:08-25

I need to use this package in node, i'm using typescript.

The example code in the doc, show this:

    var html_to_pdf = require('html-pdf-node');

let options = { format: 'A4' };
// Example of options with args //
// let options = { format: 'A4', args: ['--no-sandbox', '--disable-setuid-sandbox'] };

let file = { content: "<h1>Welcome to html-pdf-node</h1>" };
// or //
let file = { url: "https://example.com" };
html_to_pdf.generatePdf(file, options).then(pdfBuffer => {
  console.log("PDF Buffer:-", pdfBuffer);
});

but, how i can using this, in typescript context?

CodePudding user response:

Follow the tsconfig.json

structure project

{
  "compilerOptions": {
    "allowJs": true,
    "module": "CommonJS",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2021",
    "resolveJsonModule": true,
    "lib": ["es6"],
    "forceConsistentCasingInFileNames": true,
    "removeComments": true,
    "rootDirs": ["./src", "./tests"],
    "skipLibCheck": true,
    "typeRoots": [
      "./node_modules/@types",
      "./src/@types"
    ],
    "esModuleInterop": true,
    "baseUrl": ".",
    "paths": {
      "@components/*": ["./src/components/*"],
      "@configurations/*": ["./src/configurations/*"],
      "@controllers/*": ["./src/controllers/*"],
      "@entities/*": ["./src/entities/*"],
      "@helpers/*": ["./src/helpers/*"],
      "@repositories/*": ["./src/repositories/*"],
      "@services/*": ["./src/services/*"],
      "@utils/*": ["./src/utils/*"]
    }
  },
  "typeAcquisition": {
    "include": [
      "jest"
    ]
  },
  "compileOnSave": true,
  "include": [
    "src",
    "tests"
  ],
  "exclude": [
    "lib",
    "node_modules"
  ]
}

CodePudding user response:

Your library doesn't support typescript and there is no @types/html-pdf-node package, so you must create the so-called declaration file yourself.

The declaration file should be named something like html-pdf-node.d.ts and its content should start with:

declare module "html-pdf-node"{
}

Now inside the module you declare the functions the library exports, let's take your generatePdf function and see how to declare it, first it's obvious it takes two parameters and returns a promise, so we start with this:

declare module "html-pdf-node"{
    export function generatePdf(file: IFile,options: IOptions) => Promise<Buffer>;
}

So generatePdf takes two parameters, first is of type IFile which we're going to define in the same module above later, the same goes for IOptions, the function returns Promise<Buffer> where Buffer is NodeJS's Buffer.

Now to define IFile interface like this:

declare module "html-pdf-node"{
   interface IFile{
       content: string;
   }
   export function generatePdf(file: IFile,options: IOptions) => Promise<Buffer>;
}

You define IOptions in the same way and Voilà! Your library is now type safe, the file html-pdf-node.d.ts should be put in a @types folder in the root project directory.

But wait, IFile supports options other than just content?!! Well you only define what you need to use not everything the library supports, so you need to use other options? Just change IFile, did a new version of the library change generatePdf to take three parameters and not accept two? Just change its type in html-pdf-node.d.ts.

Anyway I've put all this for you to learn how to deal with a mandatory library that doesn't have any typings defined in the community, the better approach for your case is to switch to another library that is maintained and maybe has typescript built-in or at least @types/some-library package.

  • Related