zoukankan      html  css  js  c++  java
  • [Tools] Convert SVG to a PDF in Node with PDFKit and SVG.js

    Given a epxress application and an svg template, we want to draw some text, date onto it and convert it to pdf as output.

    const fs = require("fs")
    const PDFDocument = require("pdfkit")
    const SVGtoPDF = require("svg-to-pdfkit")
    
    const window = require("svgdom")
    const document = window.document
    const SVG = require("svg.js")(window)
    
    const express = require("express")
    const app = express()
    
    const background = fs
      .readFileSync("./background.svg")
      .toString()
    
    app.get("/", (req, res) => {
      const { name, date } = req.query
    
      const doc = new PDFDocument({
        layout: "landscape",
        size: "A4"
      })
    
      const draw = SVG(document.documentElement)
    
      const nameSVG = draw
        .text(name)
        .size(45)
        .attr("x", "50%")
        .attr("y", "45%")
        .attr("text-anchor", "middle")
    
      const dateSVG = draw
        .text(date)
        .size(19)
        .attr("x", "13.9%")
        .attr("y", "87.7%")
    
      SVGtoPDF(doc, background)
      SVGtoPDF(doc, nameSVG.svg())
      SVGtoPDF(doc, dateSVG.svg())
    
      doc.pipe(res)
      doc.end()
    })
    
    app.listen(3000)

    Code: Github

  • 相关阅读:
    属性MyBank
    C#语法
    NTE与C#
    css3制作网页动画
    网页定位元素
    使用ADO.NET访问数据库
    连接查询和分组查询
    模糊查询和聚合函数
    习题集
    用sql语句操作数据
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9916063.html
Copyright © 2011-2022 走看看