Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions lib/document.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions lib/mixins/pdfa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) === '-') {
Expand Down
53 changes: 53 additions & 0 deletions tests/unit/bundling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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');
});
});
Loading