📜  html 到 pdf nodejs - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:28.655000             🧑  作者: Mango

代码示例1
import fs from 'fs'
import path from 'path'
import ejs from 'ejs'
import htmlPdf from 'html-pdf'
;(async function () {
  const pdfTemplate = await ejs.renderFile(
    path.join(__dirname, './src/services/email/pdfInvoice.ejs'),
    {
      invoiceNumber: 201543502291,
      date: new Date().toLocaleDateString(),
      pickUpDatetime: new Date().toLocaleDateString(),
      returnDatetime: new Date().toLocaleDateString(),
      pickUpLocation: 'jakarta',
      returnLocation: 'jakarta',
      payments: [{ description: 'oke', durationPerHours: 20, rentPerHours: 10, amount: 2000 }],
      discount: 'RM ' + 1000,
      totalPayment: 'RM ' + 5000,
      fullName: 'john doe',
      phoneNumber: '+6287887242891'
    },
    {
      beautify: true,
      async: true
    }
  )

  htmlPdf
    .create(pdfTemplate, {
      format: 'A4',
      httpHeaders: { 'content-type': 'application/pdf' },
      quality: '100',
      orientation: 'portrait',
      type: 'pdf'
    })
    .toFile(path.join(__dirname, 'index.pdf'), (err, res) => {
      if (!err) {
        console.log(res.filename)
      }
    })
})()