Skip to content

Commit

Permalink
Merge branch 'master' into pr/1492
Browse files Browse the repository at this point in the history
  • Loading branch information
liborm85 committed Dec 24, 2024
2 parents f90b1fb + 7527b7a commit 1e365b5
Show file tree
Hide file tree
Showing 77 changed files with 3,603 additions and 5,092 deletions.
6 changes: 3 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"modules": false,
"targets": {
"node": "6.10"
"node": "18"
}
}
]
Expand All @@ -17,11 +17,11 @@
"@babel/preset-env",
{
"targets": {
"node": "6.10"
"node": "18"
}
}
]
]
}
}
}
}
25 changes: 12 additions & 13 deletions .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: yarn --frozen-lockfile
- name: Run ESLint
run: npm run lint
- name: Run tests
run: npm test
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: yarn --frozen-lockfile
- name: Run ESLint
run: npm run lint
- name: Run tests
run: npm test
3 changes: 0 additions & 3 deletions .prettierrc

This file was deleted.

23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,32 @@

### Unreleased

- Update fontkit to 2.0
- Update linebreak to 1.1

### [v0.15.2] - 2024-12-15

- Fix index not counting when rendering ordered lists (#1517)
- Fix PDF/A3 compliance of attachments
- Fix CIDSet generation only for PDF/A1 subset
- Fix missing acroform font dictionary
- Fix modify time comparison check equality embedded files

### [v0.15.1] - 2024-10-30

- Fix browserify transform sRGB_IEC61966_2_1.icc file
- Fix time comparison check equality embedded files

### [v0.15.0] - 2024-03-23

- Add subset for PDF/UA
- Fix for line breaks in list items (#1486)
- Fix for soft hyphen not being replaced by visible hyphen if necessary (#457)
- Optimize output files by ignoring identity transforms
- Fix for Acroforms - setting an option to false will still apply the flag (#1495)
- Fix for text extraction in PDFium-based viewers due to invalid ToUnicodeMap (#1498)
- Remove deprecated `write` method
- Drop support for Node.js < 18 and for browsers released before 2020

### [v0.14.0] - 2023-11-09

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ complex documents with a very small amount of code. For more, see the `demo` fol
There are three ways to use PDFKit in the browser:

- Use [Browserify](http://browserify.org/). See demo [source code](demo/browser.js) and [build script](https://github.com/foliojs/pdfkit/blob/master/package.json#L56)
- Use [webpack](https://webpack.js.org/). See [complete example](https://github.com/blikblum/pdfkit-webpack-example).
- Use [webpack](https://webpack.js.org/). See [complete example](examples/webpack).
- Use prebuilt version. Distributed as `pdfkit.standalone.js` file in the [releases](https://github.com/foliojs/pdfkit/releases) or in the package `js` folder.

In addition to PDFKit, you'll need somewhere to stream the output to. HTML5 has a
Expand Down
1 change: 1 addition & 0 deletions docs/attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ There are a few other options for `doc.file`:
* `hidden` - if true, do not show file in the list of embedded files
* `creationDate` - override the date and time the file was created
* `modifiedDate` - override the date and time the file was last updated
* `relationship` - relationship between the PDF document and its attached file. Can be 'Alternative', 'Data', 'Source', 'Supplement' or 'Unspecified'.

If you are attaching a file from your file system, creationDate and modifiedDate will be set to the source file's creationDate and modifiedDate.

Expand Down
2 changes: 1 addition & 1 deletion docs/generate_website.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const generateImages = function(tree) {
// write the PDF, convert to PNG and trim with ImageMagick (https://imagemagick.org)
file.on('finish', () => {
exec(
`magick convert -density 150x150 -trim ${f}.pdf ${f}.png`,
`magick -density 150x150 ${f}.pdf -trim ${f}.png`,
(err, stdout, stderr) => {
if (stderr) {
console.error(stderr);
Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ in your JavaScript source file and create an instance of the
`PDFDocument` class.

const PDFDocument = require('pdfkit');
const doc = new PDFDocument;
const doc = new PDFDocument();

`PDFDocument` instances are readable Node streams. They don't get saved anywhere automatically,
but you can call the `pipe` method to send the output of the PDF document to another
Expand Down
20 changes: 20 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import globals from "globals";
import js from "@eslint/js";

export default [
js.configs.recommended,
{
languageOptions: {
globals: globals.node,
sourceType: "module",
},
},
{
files: ["tests/**/*.js"],
languageOptions: {
globals: {
...globals.jest,
},
}
},
];
Binary file modified examples/attachment.pdf
Binary file not shown.
Binary file modified examples/form.pdf
Binary file not shown.
Binary file modified examples/kitchen-sink-accessible.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/kitchen-sink.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var PDFDocument = require('../js/pdfkit');
var PDFDocument = require('../');
var tiger = require('./tiger');
var fs = require('fs');

Expand Down
Binary file modified examples/kitchen-sink.pdf
Binary file not shown.
Binary file modified examples/text-link.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/webpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Simple example of using PdfKit with webpack
[`webpack.config.js`](webpack.config.js)

- add alias to map `fs` calls to pdfkit virtual file system [implementation](../../lib/virtual-fs.js)
- ignore iconv-lite and crypto packages to save bundle file size
- ignore crypto package to save bundle file size
- add aliases to native node packages (buffer, stream, zlib, util, assert)
- configure `*.afm` files to be imported as text
- configure all files in `src/static-assets` folder to be imported encoded as base64
Expand Down
15 changes: 7 additions & 8 deletions examples/webpack/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
{
"license": "MIT",
"dependencies": {
"assert": "^2.0.0",
"assert": "^2.1.0",
"brace": "^0.11.1",
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
"pdfkit": "^0.12.3",
"pdfkit": "^0.15.0",
"process": "^0.11.10",
"readable-stream": "^3.6.0",
"util": "^0.12.4"
"readable-stream": "^4.5.2",
"util": "^0.12.5"
},
"devDependencies": {
"brfs": "^2.0.2",
"html-webpack-plugin": "^5.3.2",
"html-webpack-plugin": "^5.6.0",
"transform-loader": "^0.2.4",
"webpack": "^5.44.0",
"webpack-cli": "^4.7.2"
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4"
},
"scripts": {
"dev": "webpack --mode development",
Expand Down
22 changes: 2 additions & 20 deletions examples/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ module.exports = {
})
],
resolve: {
symlinks: false,
alias: {
// maps fs to a virtual one allowing to register file content dynamically
fs: 'pdfkit/js/virtual-fs.js',
// iconv-lite is used to load cid less fonts (not spec compliant)
'iconv-lite': false
fs: __dirname + '/../../js/virtual-fs.js'
},
fallback: {
// crypto module is not necessary at browser
Expand Down Expand Up @@ -48,23 +47,6 @@ module.exports = {
{
test: /src[/\\]lazy-assets/,
type: 'asset/resource'
},
// convert to base64 and include inline file system binary files used by fontkit and linebreak
{
enforce: 'post',
test: /fontkit[/\\]index.js$/,
loader: 'transform-loader',
options: {
brfs: {}
}
},
{
enforce: 'post',
test: /linebreak[/\\]src[/\\]linebreaker.js/,
loader: 'transform-loader',
options: {
brfs: {}
}
}
]
}
Expand Down
19 changes: 3 additions & 16 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ By Devon Govett
*/

import stream from 'stream';
import fs from 'fs';
import PDFObject from './object';
import PDFReference from './reference';
import PDFPage from './page';
Expand Down Expand Up @@ -220,7 +219,9 @@ class PDFDocument extends stream.Readable {
addNamedEmbeddedFile(name, ref) {
if (!this._root.data.Names.data.EmbeddedFiles) {
// disabling /Limits for this tree fixes attachments not showing in Adobe Reader
this._root.data.Names.data.EmbeddedFiles = new PDFNameTree({ limits: false });
this._root.data.Names.data.EmbeddedFiles = new PDFNameTree({
limits: false
});
}

// add filespec to EmbeddedFiles
Expand Down Expand Up @@ -270,20 +271,6 @@ class PDFDocument extends stream.Readable {
}
}

write(filename, fn) {
// print a deprecation warning with a stacktrace
const err = new Error(`\
PDFDocument#write is deprecated, and will be removed in a future version of PDFKit. \
Please pipe the document into a Node stream.\
`);

console.warn(err.stack);

this.pipe(fs.createWriteStream(filename));
this.end();
return this.once('end', fn);
}

end() {
this.flushPages();

Expand Down
18 changes: 12 additions & 6 deletions lib/font/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ class EmbeddedFont extends PDFFont {
fontFile.data.Subtype = 'CIDFontType0C';
}

this.subset
.encodeStream()
.on('data', data => fontFile.write(data))
.on('end', () => fontFile.end());
fontFile.end(this.subset.encode());

const familyClass =
((this.font['OS/2'] != null
Expand Down Expand Up @@ -183,7 +180,7 @@ class EmbeddedFont extends PDFFont {
descriptor.data.FontFile2 = fontFile;
}

if (this.document.subset) {
if (this.document.subset && this.document.subset === 1) {
const CIDSet = Buffer.from('FFFFFFFFC0', 'hex');
const CIDSetRef = this.document.ref();
CIDSetRef.write(CIDSet);
Expand Down Expand Up @@ -252,6 +249,15 @@ class EmbeddedFont extends PDFFont {
entries.push(`<${encoded.join(' ')}>`);
}

const chunkSize = 256;
const chunks = Math.ceil(entries.length / chunkSize);
const ranges = [];
for (let i = 0; i < chunks; i++) {
const start = i * chunkSize;
const end = Math.min((i + 1) * chunkSize, entries.length);
ranges.push(`<${toHex(start)}> <${toHex(end - 1)}> [${entries.slice(start, end).join(' ')}]`);
}

cmap.end(`\
/CIDInit /ProcSet findresource begin
12 dict begin
Expand All @@ -267,7 +273,7 @@ begincmap
<0000><ffff>
endcodespacerange
1 beginbfrange
<0000> <${toHex(entries.length - 1)}> [${entries.join(' ')}]
${ranges.join('\n')}
endbfrange
endcmap
CMapName currentdict /CMap defineresource pop
Expand Down
8 changes: 3 additions & 5 deletions lib/font_factory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import fontkit from 'fontkit';
import * as fontkit from 'fontkit';
import StandardFont from './font/standard';
import EmbeddedFont from './font/embedded';

Expand All @@ -13,12 +13,10 @@ class PDFFontFactory {

src = fs.readFileSync(src);
}
if (Buffer.isBuffer(src)) {
if (src instanceof Uint8Array) {
font = fontkit.create(src, family);
} else if (src instanceof Uint8Array) {
font = fontkit.create(Buffer.from(src), family);
} else if (src instanceof ArrayBuffer) {
font = fontkit.create(Buffer.from(new Uint8Array(src)), family);
font = fontkit.create(new Uint8Array(src), family);
}

if (font == null) {
Expand Down
8 changes: 5 additions & 3 deletions lib/mixins/acroform.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export default {
_fieldDict(name, type, options = {}) {
if (!this._acroform) {
throw new Error(
'Call document.initForms() method before adding form elements to document'
'Call document.initForm() method before adding form elements to document'
);
}
let opts = Object.assign({}, options);
Expand Down Expand Up @@ -299,7 +299,9 @@ export default {
let result = 0;
Object.keys(options).forEach(key => {
if (FIELD_FLAGS[key]) {
result |= FIELD_FLAGS[key];
if (options[key]) {
result |= FIELD_FLAGS[key];
}
delete options[key];
}
});
Expand All @@ -326,7 +328,7 @@ export default {

_resolveFont(options) {
// add current font to document-level AcroForm dict if necessary
if (this._acroform.fonts[this._font.id] === null) {
if (this._acroform.fonts[this._font.id] == null) {
this._acroform.fonts[this._font.id] = this._font.ref();
}

Expand Down
Loading

0 comments on commit 1e365b5

Please sign in to comment.