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

  • 相关阅读:
    java大数取余
    hdu--5351--MZL's Border
    NYOJ--水池数目
    NYOJ--32--SEARCH--组合数
    NYOJ--20--搜索(dfs)--吝啬的国度
    hdu--4148--Length of S(n)
    hdu--2098--分拆素数和
    hdu--1873--看病要排队
    hdu--1870--愚人节的礼物
    hdu--1237--简单计算器
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9916063.html
Copyright © 2011-2022 走看看