[Tools] Convert SVG to a PDF in Node with PDFKit and SVG.js
2024-08-30 00:53:30
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()
.attr("x", "50%")
.attr("y", "45%")
.attr("text-anchor", "middle") const dateSVG = draw
.text(date)
.size()
.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()
Code: Github
最新文章
- jquery:validate的例子
- Python实现各种排序算法的代码示例总结
- <;转>;.php导出excel(多种方法)
- Xamarin Anroid开发教程之验证环境配置是否正确
- android AsyncHttpClient 开源框架的使用
- 一些CSS技巧
- 一个loader加载多个swf
- 【C疯狂的教材】(四)C语言分支语句
- @dynamic、@synthesize
- Java并发Fork-Join框架原理解析
- Source Insight4
- mac中:不能完成此操作,因为找不到一个或多个需要的项目。(错误代码 -43)
- iview render bug &; vue namespace bug
- github &; markdown &; collapse &; table
- 将GETDATE()转换为指定日期格式的varchar类型
- #ifdef __cplusplus extern "C" { #endif 含义
- Android 加载大图
- 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat
- [Jobdu] 题目1370:数组中出现次数超过一半的数字
- 201621123006 《Java程序设计》第5周学习总结
热门文章
- NGINX宏观手记
- Django ORM操作及进阶
- 总线(bus);设备(devices);驱动(drivers)
- UVa 11987 并查集 Almost Union-Find
- 【0门槛】PR稿的自我修养
- wp8 longlistselector 动态加载datatemplate
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
- 2.ruby基本语法,类的定义
- Python GUI 之 Treeview 学习
- BZOJ 2438 [中山市选2011]杀人游戏 ——期望DP