[breaking]: FreeSewing v4 (#7297)
Refer to the CHANGELOG for all info. --------- Co-authored-by: Wouter van Wageningen <wouter.vdub@yahoo.com> Co-authored-by: Josh Munic <jpmunic@gmail.com> Co-authored-by: Jonathan Haas <haasjona@gmail.com>
This commit is contained in:
parent
d22fbe78d9
commit
51dc1d9732
6626 changed files with 142053 additions and 150606 deletions
59
packages/react/components/Editor/lib/export/pdf.mjs
Normal file
59
packages/react/components/Editor/lib/export/pdf.mjs
Normal file
|
@ -0,0 +1,59 @@
|
|||
import PDFDocument from 'pdfkit/js/pdfkit.standalone.js'
|
||||
|
||||
/**
|
||||
* PdfKit, the library we're using for pdf generation, uses points as a unit,
|
||||
* so when we tell it things like where to put the svg and how big the svg is,
|
||||
* we need those numbers to be in points. The svg uses mm internally, so when
|
||||
* we do spatial reasoning inside the svg, we need to know values in mm
|
||||
* */
|
||||
export const mmToPoints = 2.834645669291339
|
||||
|
||||
/**
|
||||
* A PDFKit PDF instance
|
||||
*/
|
||||
export const Pdf = ({ size, layout }) => {
|
||||
const pdf = new PDFDocument({
|
||||
size,
|
||||
layout,
|
||||
})
|
||||
|
||||
/*
|
||||
* PdfKit wants to flush the buffer on each new page.
|
||||
* We can't save directly from inside a worker, so we have to manage the
|
||||
* buffers ourselves so we can return a blob
|
||||
*/
|
||||
const buffers = []
|
||||
|
||||
/*
|
||||
* Use a listener to add new data to our buffer storage
|
||||
*/
|
||||
pdf.on('data', buffers.push.bind(buffers))
|
||||
|
||||
/*
|
||||
* Convert the pdf to a blob
|
||||
*/
|
||||
pdf.toBlob = function () {
|
||||
return new Promise((resolve) => {
|
||||
/*
|
||||
* We have to do it this way so that the document flushes everything to buffers
|
||||
*/
|
||||
pdf.on('end', () => {
|
||||
/*
|
||||
* Convert buffers to a blob
|
||||
*/
|
||||
resolve(
|
||||
new Blob(buffers, {
|
||||
type: 'application/pdf',
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
/*
|
||||
* End the stream
|
||||
*/
|
||||
pdf.end()
|
||||
})
|
||||
}
|
||||
|
||||
return pdf
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue