Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"mitt": "^3.0.1",
"motion": "^12.34.3",
"nanoid": "^5.1.6",
"pdfkit": "^0.19.1",
"pdfkit": "^0.20.2",
"tailwind-merge": "^3.5.0",
"three-bvh-csg": "^0.0.18",
"three-mesh-bvh": "~0.9.8",
Expand Down
37 changes: 27 additions & 10 deletions packages/editor/src/lib/floorplan/floorplan-pdfkit-document.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/// <reference path="./pdfkit-standard-fonts.d.ts" />
import type PdfKitDocument from 'pdfkit'

type PdfKitDocumentInstance = InstanceType<typeof PdfKitDocument>

/** pdfkit 0.20's browser-build export, which @types/pdfkit does not describe. */
type PdfKitStandardFontRegistry = { registerStdFonts?: (...fonts: unknown[]) => void }

type PdfTextOptions = {
align?: 'left' | 'center' | 'right'
maxWidth?: number
Expand Down Expand Up @@ -461,16 +465,29 @@ export async function createFloorplanPdfDocument(
defaultPageSize: readonly [number, number],
options: { title?: string } = {},
) {
const [{ default: PDFDocument }, { default: blobStream }, fonts] = await Promise.all([
import('pdfkit/js/pdfkit.standalone'),
import('blob-stream'),
// an export in the standard fonts beats no export when the font files are unreachable
loadFloorplanPdfFonts().catch((error: unknown) => {
console.warn('[floorplan-export] Falling back to the standard PDF fonts', error)
return null
}),
])
const raw = new PDFDocument({
const [pdfkit, helvetica, helveticaBold, courier, courierBold, { default: blobStream }, fonts] =
await Promise.all([
import('pdfkit'),
import('pdfkit/standard-fonts/Helvetica'),
import('pdfkit/standard-fonts/HelveticaBold'),
import('pdfkit/standard-fonts/Courier'),
import('pdfkit/standard-fonts/CourierBold'),
import('blob-stream'),
// an export in the standard fonts beats no export when the font files are unreachable
loadFloorplanPdfFonts().catch((error: unknown) => {
console.warn('[floorplan-export] Falling back to the standard PDF fonts', error)
return null
}),
])
// The browser build has no standard-font metrics until they are registered, and
// the constructor already sets Helvetica; the Node build loads its own and has no registry.
;(pdfkit as PdfKitStandardFontRegistry).registerStdFonts?.(
helvetica.default,
helveticaBold.default,
courier.default,
courierBold.default,
)
const raw = new pdfkit.default({
autoFirstPage: false,
compress: true,
margin: 0,
Expand Down
5 changes: 5 additions & 0 deletions packages/editor/src/lib/floorplan/pdfkit-standard-fonts.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// pdfkit 0.20 ships these entry points without types, and @types/pdfkit predates them.
declare module 'pdfkit/standard-fonts/*' {
const font: unknown
export default font
}
Loading