[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 ...
随机推荐
- Spring容器的理解
控制反转(IOC,Inversion Of Contorl)和依赖注入(DI,Dependency Injection) IOC是一个宽泛的概念:对某事物的控制权由A转移到B. DI是实现IOC的一种 ...
- verilog behaviral modeling -- procedural timing contronls
1.delay control : an expression specifies the time duration between initially encountering the state ...
- centos7.2 安装nginx
一.首先检查gcc编译器安装了没 二 首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库),检查一下是否已经安装了( ...
- ASP.NET使用Memcached高缓存实例的初级介绍
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...
- spring-cloud-sleuth 学习资源
https://www.baeldung.com/spring-cloud-sleuth-single-application https://howtodoinjava.com/spring-clo ...
- sequence有关问题
sequence问题比如主键是 1,3,5,7,9,11 中间跳号了...用什么方法可以把主键重新排列为 1,2,3,4,5 ------解决方案--------------------update ...
- 【转】Python3学习笔记(urllib模块的使用)
原文地址:https://www.cnblogs.com/Lands-ljk/p/5447127.html 1.基本方法 urllib.request.urlopen(url, data=None, ...
- 14-new和this
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- iOS代理转移
在控制器里面想调用一个视图的子视图的代理方法很简单 1.找到子视图的协议和代理属性 2.给你想调用的控件添加代理属性,遵守的协议和子视图的一样 3.重写代理属性的set方法
- P3694 邦邦的大合唱站队 (状压DP)
题目背景 BanG Dream!里的所有偶像乐队要一起大合唱,不过在排队上出了一些问题. 题目描述 N个偶像排成一列,他们来自M个不同的乐队.每个团队至少有一个偶像. 现在要求重新安排队列,使来自同一 ...