[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 ...
随机推荐
- (转)ios应用导航模型
Eko - MoboCentre 本文将介绍iPhone的导航风格,同时,也一并了解能够组织好应用内容和工具的导航方式.对于一个应用来说,最基础的操作就是基于页面间简单的移动,每张页面都完成一个任务或 ...
- (原)UICollectionView的基本使用
UICollectionView基本使用 学习iOS一段时间了,早听说了UICollectionView的强大一直没有机会使用,今天自己研究了一下. UICollectonView类似UITableV ...
- LeetCode(1)Two Sum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- python 配置opencv-python 接口
anaconda2下配置opencv-python 接口,import cv2遇到no cv2 模块问题,解决办法是将cv2.so放到anaconda2/lib/python2.7/site-pack ...
- python linux安装anaconda
步骤: 1.在清华大学镜像站中下载anaconda版本:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ https://mirrors.t ...
- 【Luogu】P1072Hankson的趣味题(gcd)
这题真TM的趣味. 可以说我的动手能力还是不行,想到了算法却写不出来.以后说自己数论会GCD的时候只好虚了…… 我们首先这么想. x与a0的最大公约数为a1,那么我们把x/=a1,a0/=a1之后,x ...
- 暑假训练Round1——G: Hkhv的水题之二(字符串的最小表示)
Problem 1057: Hkhv的水题之二 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: ...
- 算法复习——差分约束(ssoi种树)
题目: 题目描述 为了绿化乡村,H 村积极响应号召,开始种树了. H 村里有 n 幢房屋,这些屋子的排列顺序很有特点,在一条直线上.于是方便起见,我们给它们标上 1-n .树就种在房子前面的空地上. ...
- 学习shell脚本之乘法口诀
脚本如下: #!/bin/bash for i in 'seq 1 9' do for j in 'seq 1 $i' do k=$[$i*$j] echo -ne "$j"&qu ...
- Spring boot 中 Spring Security 使用改造5部曲(转)
文章的内容有点长,也是自己学习Spring security的一个总结.如果你从头看到尾,我想你对Spring Security的使用和基本原理应该会有一个比较清晰的认识. 如果有什么理解不对的地方, ...