[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()
.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
[Tools] Convert SVG to a PDF in Node with PDFKit and SVG.js的更多相关文章
- convert:图片转pdf失败
造冰箱的大熊猫@cnblogs 2019/1/21 1.环境 操作系统/Ubuntu 16.04.5 LTS,内核/4.15.0-43-generic,convert/ImageMagick 6.8 ...
- [javascript svg fill stroke stroke-width rx ry ellipse 属性讲解] svg fill stroke stroke-width ellipse 绘制椭圆属性讲解
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...
- [Tools] Create a Simple CLI Tool in Node.js with CAC
Command-line tools can help you with all sorts of tasks. This lesson covers the very basics of setti ...
- Java 将PDF 转为Word、图片、SVG、XPS、Html、PDF/A
本文将介绍通过Java编程来实现PDF文档转换的方法.包括: 1. PDF转为Word 2. PDF转为图片 3. PDF转为Html 4. PDF转为SVG 4.1 将PDF每一页转为单个的SVG ...
- 【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
SVG是一种图形文件格式,它的英文全称为Scalable Vector Graphics,意思为可缩放的矢量图形.它在放大或者改变尺寸的情况下其图形质量不会有所损失,且与 JPG 和 GIF 图像比起 ...
- 非node环境下的vue.js 实现简单的购物车计算功能 样式请无视
都说vue的双向数据绑定好用,自己用了下,感觉做购物车没想象中好用.自己的实现如下: <!DOCTYPE html> <html lang="en"> &l ...
- node 模块化思想中index.js的重要性
目录结构如上图 module1和modlue2.main在同一级 module1下文件: index.js var test2=require('./test2'); var sayHi=functi ...
- 启动node程序报错:event.js:183 throw er; // unhandled 'error' event
启动node程序时,报如下错误:
- svg web拓扑更新了,支持动态添加svg组件
版本1.0请点此 预览地址 https://svg.yaolunmao.top 如何使用 # 克隆项目 git clone https://github.com/yaolunmao/vue-webto ...
随机推荐
- 对于js运动中产生的问题
1.不同的对象调用同一个定时器情况,则需要将定时器的名称定为该对象的一个属性来进行运用. 例: <!DOCTYPE html> <html lang="en"&g ...
- Solr5.0.0 DIH之增量索引
定时索引相关知识 增量更新需要配置个sql(deltaImportQuery.deltaQuery) deltaImportQuery="select * where id='${dih.d ...
- 每周一题 3n+1问题
3n+1问题 #include<iostream> #include<math.h> #include<map> using namespace std; map& ...
- HUD--2553 N皇后问题
Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N, ...
- python知识点拾遗
内容概要 1.__str__ 2.os.path相关方法 1.__str__ 我们先定义一个Student类,打印一个实例: class Student(object): def __init__(s ...
- Hive中文注释乱码解决方案
本文来自网易云社区 作者:王潘安 快速解决方法 目前的hive客户端在执行desc tablexxx和show create table xxx命令的时候,字段的中文注释会出现乱码情况,如(????) ...
- Linux基础命令详解-1
本篇详解的命令有以下30个 1.cd 功能:切换工作目录 参数列表 2.ls 功能:查看目录里的内容 参数列表 3.mv 功能: 移动或重命名文件和目录 参数列表 4.pwd ...
- Thread.getContextClassLoader() is null
Java threads created from JNI code in a non-java thread have null ContextClassloader unless the crea ...
- LDP协议详解-上
MPLS基础 模式 标签分配模式(label allocation)本地为一条路由前缀绑定一个label标签的条件.独立控制模式(independent control)本地RIB学习到的路由(除BG ...
- xmpp 登录注册小结
将XMPPStream放在APPDelegate,以便全局访问 #pragma mark - XMPP相关的属性和方法定义 /** * 全局xmppstream,只读属性 */ @property ( ...