Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@nxmix/svg-to-pdfkit2
Advanced tools
import SVGtoPDF from "svg-to-pdfkit";
import { load } from "color-books";
const getColor = (bookName, colorName: string) {
const book = load(bookName);
if (book.records[colorName]) {
return {
name: colorName,
colorSpace: book.colorSpace,
components: book.records[colorName].components,
isSpot: book.isSpot
};
}
};
// <path d="..." fill="COLOR_BOOK(PANTONE+ CMYK Coated/PANTONE P 24-8 C)"></path>
const parseColorCallback = (color) => {
const temp = color.match(/^COLOR_BOOK\((.+)\/(.+)\)$/);
if (temp) {
const [bookName, colorName] = temp.slice(1);
return getColor(bookName, colorName);
}
};
const draw = (doc, svg, { left, top, width, height }) => {
doc.save();
try {
SVGtoPDF(doc, svg, left, top, {
width,
height,
parseColorCallback
});
} finally {
doc.restore();
}
};
fs = require 'fs'
PDFDocument = require 'pdfkit'
SVGtoPDF = require 'svg-to-pdfkit'
open = require 'opn'
cheerio = require 'cheerio'
css = require 'css'
doc = new PDFDocument autoFirstPage: false
###
<svg>
<defs>
<style>
.cls-1 {
fill: red;
}
.cls-2 {
stroke: yellow;
}
</style>
</defs>
<rect class="cls-1 cls-2" x="0" y="0" width="100" height="100" />
</svg>
###
svgContent = fs.readFileSync 'has-css.svg', 'utf8'
$ = cheerio.load svgContent, xmlMode: true
styles = reduce.call $('svg defs style').contents(), (sum, curr) ->
if curr.type is 'text'
sum += curr.data + '\n'
sum
, ''
parsed = css.parse(styles)
cssProps = parsed.stylesheet.rules.reduce (propsBySelector, rule) ->
if rule.type is 'rule'
props = rule.declarations.reduce (sum, decor) ->
sum[decor.property] = decor.value
sum
, {}
for s in rule.selectors
propsBySelector[s] = props
propsBySelector
, {}
doc.pipe fs.createWriteStream 'output.pdf'
doc.addPage { size: [512, 512], margin: 0 }
getComputedStyleCallback = (node) ->
if node.getAttribute('class')
classNames = node.getAttribute('class').split(/\s/)
styles = classNames.reduce((sum, name) ->
name = '.' + name
if cssProps[name]
sum = { sum..., cssProps[name]... }
sum
, {})
styles
SVGtoPDF doc, svgContent, 0, 0, { width: 200, height: 200, getComputedStyleCallback }
doc.end();
Insert SVG into a PDF document created with PDFKit.
SVGtoPDF(doc, svg, x, y, options);
If you prefer, you can add the function to the PDFDocument prototype:
PDFDocument.prototype.addSVG = function(svg, x, y, options) {
return SVGtoPDF(this, svg, x, y, options), this;
};
And then simply call:
doc.addSVG(svg, x, y, options);
doc [PDFDocument] = the PDF document created with PDFKit
svg [SVGElement or string] = the SVG object or XML code
x, y [number] = the position where the SVG will be added
options [Object] = >
- width, height [number] = initial viewport, by default it's the page dimensions
- preserveAspectRatio [string] = override alignment of the SVG content inside its viewport
- useCSS [boolean] = use the CSS styles computed by the browser (for SVGElement only)
- fontCallback [function] = function called to get the fonts, see source code
- imageCallback [function] = same as above for the images (for Node.js)
- warningCallback [function] = function called when there is a warning
- assumePt [boolean] = assume that units are PDF points instead of SVG pixels
- precision [number] = precision factor for approximative calculations (default = 3)
https://alafr.github.io/SVG-to-PDFKit/examples/demo.htm
https://alafr.github.io/SVG-to-PDFKit/examples/options.htm
FAQs
Insert SVG into a PDF document created with PDFKit
We found that @nxmix/svg-to-pdfkit2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.