[Tools] Convert SVG to a PDF in Node with PDFKit and SVG.js
2023-09-14 09:00:49 时间
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
相关文章
- JS框架_(Progress.js)圆形动画进度条
- js的alert和confirm美化
- 微信QQ的二维码登录原理js代码解析
- [Vue] Create Filters in Vue.js
- nginx js、css多个请求合并为一个请求(concat模块)
- JS base64编码和解码
- 如何查看某个js 变量 runtime 类型
- atitit.atiOrm.js v2 q61 版本新特性.docx
- js字符串的截取
- 【Auto.js】使用命令删除图片后,更新图库缓存
- 一统江湖的大前端(2)—— Mock.js + Node.js 如何与后端潇洒分手
- js 模块化基础和模块规范AMD、CMD、ES6模块
- js 实现二叉树前序遍历
- 一分钟理解js闭包