fix(shared): Import logo from the correct place
This commit is contained in:
parent
4ea391ba91
commit
7c4565b52a
1 changed files with 154 additions and 154 deletions
|
@ -1,11 +1,11 @@
|
||||||
import PDFDocument from 'pdfkit/js/pdfkit.standalone'
|
import PDFDocument from 'pdfkit/js/pdfkit.standalone'
|
||||||
import SVGtoPDF from 'svg-to-pdfkit'
|
import SVGtoPDF from 'svg-to-pdfkit'
|
||||||
import {logo} from '@freesewing/plugin-logo'
|
import { path as logoPath } from '../icons/freesewing.js'
|
||||||
|
|
||||||
/** an svg of the logo to put on the cover page */
|
/** an svg of the logo to put on the cover page */
|
||||||
const logoSvg = `<svg viewBox="-22 -36 46 50">
|
const logoSvg = `<svg viewBox="-22 -36 46 50">
|
||||||
<style> path {fill: none; stroke: #555555; stroke-width: 0.5} </style>
|
<style> path {fill: none; stroke: #555555; stroke-width: 0.5} </style>
|
||||||
${logo}
|
<path d=${logoPath} />
|
||||||
</svg>`
|
</svg>`
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,9 +48,9 @@ export default class PdfMaker {
|
||||||
|
|
||||||
pageCount = 0
|
pageCount = 0
|
||||||
|
|
||||||
constructor({svg, settings, pages, strings}) {
|
constructor({ svg, settings, pages, strings }) {
|
||||||
this.settings = settings
|
this.settings = settings
|
||||||
this.pagesWithContent = pages.withContent;
|
this.pagesWithContent = pages.withContent
|
||||||
this.svg = svg
|
this.svg = svg
|
||||||
this.strings = strings
|
this.strings = strings
|
||||||
|
|
||||||
|
@ -69,27 +69,26 @@ export default class PdfMaker {
|
||||||
this.svgHeight = this.rows * pages.height * mmToPoints
|
this.svgHeight = this.rows * pages.height * mmToPoints
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** create the pdf document */
|
/** create the pdf document */
|
||||||
initPdf() {
|
initPdf() {
|
||||||
// instantiate with the correct size and orientation
|
// instantiate with the correct size and orientation
|
||||||
this.pdf = new PDFDocument({
|
this.pdf = new PDFDocument({
|
||||||
size: this.settings.size.toUpperCase(),
|
size: this.settings.size.toUpperCase(),
|
||||||
layout: this.settings.orientation
|
layout: this.settings.orientation,
|
||||||
})
|
})
|
||||||
|
|
||||||
// PdfKit wants to flush the buffer on each new page.
|
// 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
|
// We can't save directly from inside a worker, so we have to manage the buffers ourselves so we can return a blob
|
||||||
this.buffers = [];
|
this.buffers = []
|
||||||
|
|
||||||
// use a listener to add new data to our buffer storage
|
// use a listener to add new data to our buffer storage
|
||||||
this.pdf.on('data', this.buffers.push.bind(this.buffers));
|
this.pdf.on('data', this.buffers.push.bind(this.buffers))
|
||||||
}
|
}
|
||||||
|
|
||||||
/** make the pdf */
|
/** make the pdf */
|
||||||
async makePdf() {
|
async makePdf() {
|
||||||
await this.generateCoverPage()
|
await this.generateCoverPage()
|
||||||
await this.generatePages();
|
await this.generatePages()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** convert the pdf to a blob */
|
/** convert the pdf to a blob */
|
||||||
|
@ -98,10 +97,12 @@ export default class PdfMaker {
|
||||||
// have to do it this way so that the document flushes everything to buffers
|
// have to do it this way so that the document flushes everything to buffers
|
||||||
this.pdf.on('end', () => {
|
this.pdf.on('end', () => {
|
||||||
// convert buffers to a blob
|
// convert buffers to a blob
|
||||||
resolve(new Blob(this.buffers, {
|
resolve(
|
||||||
type: 'application/pdf'
|
new Blob(this.buffers, {
|
||||||
}))
|
type: 'application/pdf',
|
||||||
});
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
// end the stream
|
// end the stream
|
||||||
this.pdf.end()
|
this.pdf.end()
|
||||||
|
@ -110,7 +111,6 @@ export default class PdfMaker {
|
||||||
|
|
||||||
/** generate the cover page for the pdf */
|
/** generate the cover page for the pdf */
|
||||||
async generateCoverPage() {
|
async generateCoverPage() {
|
||||||
|
|
||||||
// don't make one if it's not requested
|
// don't make one if it's not requested
|
||||||
if (!this.settings.coverPage) {
|
if (!this.settings.coverPage) {
|
||||||
return
|
return
|
||||||
|
@ -129,8 +129,8 @@ export default class PdfMaker {
|
||||||
height: coverHeight,
|
height: coverHeight,
|
||||||
assumePt: false,
|
assumePt: false,
|
||||||
// use aspect ratio to center it
|
// use aspect ratio to center it
|
||||||
preserveAspectRatio: 'xMidYMid meet'
|
preserveAspectRatio: 'xMidYMid meet',
|
||||||
});
|
})
|
||||||
this.pageCount++
|
this.pageCount++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,7 +146,6 @@ export default class PdfMaker {
|
||||||
this.pdf.text(this.strings.tagline, lineStart, lineLevel)
|
this.pdf.text(this.strings.tagline, lineStart, lineLevel)
|
||||||
lineLevel += 12 + 20
|
lineLevel += 12 + 20
|
||||||
|
|
||||||
|
|
||||||
this.pdf.fontSize(48)
|
this.pdf.fontSize(48)
|
||||||
this.pdf.text(this.strings.design, lineStart, lineLevel)
|
this.pdf.text(this.strings.design, lineStart, lineLevel)
|
||||||
lineLevel += 48
|
lineLevel += 48
|
||||||
|
@ -154,11 +153,12 @@ export default class PdfMaker {
|
||||||
await SVGtoPDF(this.pdf, logoSvg, this.pdf.page.width - lineStart - 100, lineStart, {
|
await SVGtoPDF(this.pdf, logoSvg, this.pdf.page.width - lineStart - 100, lineStart, {
|
||||||
width: 100,
|
width: 100,
|
||||||
height: lineLevel - lineStart - 8,
|
height: lineLevel - lineStart - 8,
|
||||||
preserveAspectRatio: 'xMaxYMin meet'
|
preserveAspectRatio: 'xMaxYMin meet',
|
||||||
})
|
})
|
||||||
|
|
||||||
this.pdf.lineWidth(1)
|
this.pdf.lineWidth(1)
|
||||||
this.pdf.moveTo(lineStart, lineLevel - 8)
|
this.pdf
|
||||||
|
.moveTo(lineStart, lineLevel - 8)
|
||||||
.lineTo(this.pdf.page.width - lineStart, lineLevel - 8)
|
.lineTo(this.pdf.page.width - lineStart, lineLevel - 8)
|
||||||
.stroke()
|
.stroke()
|
||||||
|
|
||||||
|
@ -184,7 +184,7 @@ export default class PdfMaker {
|
||||||
for (var h = 0; h < this.rows; h++) {
|
for (var h = 0; h < this.rows; h++) {
|
||||||
for (var w = 0; w < this.columns; w++) {
|
for (var w = 0; w < this.columns; w++) {
|
||||||
// skip empty pages
|
// skip empty pages
|
||||||
if (!this.pagesWithContent[h][w]) continue;
|
if (!this.pagesWithContent[h][w]) continue
|
||||||
|
|
||||||
// position it
|
// position it
|
||||||
let x = -w * this.pageWidth + startMargin
|
let x = -w * this.pageWidth + startMargin
|
||||||
|
@ -198,7 +198,7 @@ export default class PdfMaker {
|
||||||
|
|
||||||
// add the pdf to the page, offset by the page distances
|
// add the pdf to the page, offset by the page distances
|
||||||
await SVGtoPDF(this.pdf, this.svg, x, y, options)
|
await SVGtoPDF(this.pdf, this.svg, x, y, options)
|
||||||
this.pageCount++;
|
this.pageCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue