diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf2b3cd..10a152fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Unreleased +- Fix the browser build throwing on import where `import.meta.url` is undefined, as in bundles for Cloudflare Workers. It now registers its embedded sRGB ICC profile under a fixed name instead of a URL. Fixes #1873 - Add an `onPageAdded` document option, a function called with the document each time a page is added, including the automatic first page that a `pageAdded` listener attached after construction would miss. Fixes #1529 - Fix smooth curves (`S`, `T`) in SVG paths reflecting the control point of an arc or of a curve of the other kind. As the SVG spec says, they now start from the current point there. An arc with a zero radius is now drawn as a straight line instead of nothing diff --git a/lib/document.browser.js b/lib/document.browser.js index 051e5ea8..91262d5f 100644 --- a/lib/document.browser.js +++ b/lib/document.browser.js @@ -4,10 +4,15 @@ import { PAGE_SIZES } from './page'; import { registerStdFonts } from './font/standard_fonts'; import { registerFile } from '#fs'; import { fromBase64 } from './binary'; -import { getIccProfilePath } from './mixins/pdfa'; +import { setIccProfilePath } from './mixins/pdfa'; import iccProfileBase64 from './mixins/data/sRGB_IEC61966_2_1.icc'; -registerFile(getIccProfilePath(), fromBase64(iccProfileBase64)); +// The profile is embedded, so it is registered under a fixed name rather than a +// URL resolved from `import.meta.url`, which bundlers for runtimes such as +// Cloudflare Workers leave undefined. +const iccProfilePath = 'sRGB_IEC61966_2_1.icc'; +setIccProfilePath(iccProfilePath); +registerFile(iccProfilePath, fromBase64(iccProfileBase64)); export { PDFDocument, LineWrapper, PAGE_SIZES, registerStdFonts, registerFile }; export default PDFDocument; diff --git a/lib/mixins/pdfa.js b/lib/mixins/pdfa.js index 4c106d9a..dfb4c420 100644 --- a/lib/mixins/pdfa.js +++ b/lib/mixins/pdfa.js @@ -23,6 +23,12 @@ export const getIccProfilePath = () => { return iccProfilePath; }; +// Lets a build that registers the profile itself, such as the browser build, +// name the path the profile is read from instead of resolving it. +export const setIccProfilePath = (path) => { + iccProfilePath = path; +}; + export default { initPDFA(pSubset) { if (pSubset.charAt(pSubset.length - 3) === '-') { diff --git a/tests/unit/bundling.spec.js b/tests/unit/bundling.spec.js index 3fb481aa..631402ef 100644 --- a/tests/unit/bundling.spec.js +++ b/tests/unit/bundling.spec.js @@ -11,6 +11,7 @@ import { fileURLToPath, pathToFileURL } from 'url'; import { nodeResolve } from '@rollup/plugin-node-resolve'; import { rollup } from 'rollup'; import Helvetica from '../../lib/font/generated/Helvetica'; +import binary from '../../tools/binary-plugin.mjs'; const rootDir = fileURLToPath(new URL('../..', import.meta.url)); const iccProfile = readFileSync( @@ -146,3 +147,55 @@ describe('bundled node build', () => { } }); }); + +// Bundles the browser build the way a bundler for a runtime such as Cloudflare +// Workers does: an ES module whose `import.meta.url` is undefined. +describe('bundled browser build without import.meta.url', () => { + let bundleDir; + let pdfkit; + + beforeAll(async () => { + const cacheDir = path.join(rootDir, 'node_modules/.cache'); + mkdirSync(cacheDir, { recursive: true }); + bundleDir = mkdtempSync(path.join(cacheDir, 'pdfkit-browser-bundle-')); + writeFileSync(path.join(bundleDir, 'package.json'), '{}\n'); + + const bundle = await rollup({ + input: path.join(rootDir, 'lib/document.browser.js'), + external: (id) => + !id.startsWith('.') && !id.startsWith('#') && !path.isAbsolute(id), + plugins: [binary('.icc'), nodeResolve({ exportConditions: ['default'] })], + onwarn: () => {}, + }); + + const bundleFile = path.join(bundleDir, 'pdfkit.browser.mjs'); + await bundle.write({ + file: bundleFile, + format: 'es', + plugins: [ + { name: 'no-import-meta', resolveImportMeta: () => 'undefined' }, + ], + }); + await bundle.close(); + + pdfkit = await import(pathToFileURL(bundleFile).href); + }); + + afterAll(() => { + rmSync(bundleDir, { recursive: true, force: true }); + }); + + test('loads', () => { + expect(typeof pdfkit.PDFDocument).toBe('function'); + }); + + test('renders PDF/A with the embedded ICC profile', async () => { + pdfkit.registerStdFonts(Helvetica); + + const document = new pdfkit.PDFDocument({ subset: 'PDF/A-1' }); + const output = await render(document); + + expect(output.subarray(0, 5).toString()).toBe('%PDF-'); + expect(output.toString('latin1')).toContain('/GTS_PDFA1'); + }); +});