Skip to content

Commit

Permalink
migrate away from the fork of foliojs packages
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLenglet committed Oct 25, 2022
1 parent 9f5cb8f commit 05f7121
Show file tree
Hide file tree
Showing 9 changed files with 2,742 additions and 2,624 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"test": "tests"
},
"dependencies": {
"@foliojs-fork/linebreak": "^1.1.1",
"@foliojs-fork/pdfkit": "^0.13.0",
"iconv-lite": "^0.6.3",
"linebreak": "^1.1.0",
"pdfkit": "^0.13.0",
"xmldoc": "^1.1.2"
},
"devDependencies": {
Expand Down
5,104 changes: 2,552 additions & 2,552 deletions src/3rd-party/svg-to-pdfkit/source.js

Large diffs are not rendered by default.

58 changes: 37 additions & 21 deletions src/PDFDocument.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import PDFKit from '@foliojs-fork/pdfkit';
import PDFKit from "pdfkit";

const typeName = (bold, italics) => {
let type = 'normal';
let type = "normal";
if (bold && italics) {
type = 'bolditalics';
type = "bolditalics";
} else if (bold) {
type = 'bold';
type = "bold";
} else if (italics) {
type = 'italics';
type = "italics";
}
return type;
};

class PDFDocument extends PDFKit {
constructor(fonts = {}, images = {}, patterns = {}, attachments = {}, options = {}, virtualfs = null) {
constructor(
fonts = {},
images = {},
patterns = {},
attachments = {},
options = {},
virtualfs = null
) {
super(options);

this.fonts = {};
Expand All @@ -26,7 +33,7 @@ class PDFDocument extends PDFKit {
normal: fontDef.normal,
bold: fontDef.bold,
italics: fontDef.italics,
bolditalics: fontDef.bolditalics
bolditalics: fontDef.bolditalics,
};
}
}
Expand All @@ -35,11 +42,16 @@ class PDFDocument extends PDFKit {
for (let pattern in patterns) {
if (patterns.hasOwnProperty(pattern)) {
let patternDef = patterns[pattern];
this.patterns[pattern] = this.pattern(patternDef.boundingBox, patternDef.xStep, patternDef.yStep, patternDef.pattern, patternDef.colored);
this.patterns[pattern] = this.pattern(
patternDef.boundingBox,
patternDef.xStep,
patternDef.yStep,
patternDef.pattern,
patternDef.colored
);
}
}


this.images = images;
this.attachments = attachments;
this.virtualfs = virtualfs;
Expand All @@ -61,7 +73,9 @@ class PDFDocument extends PDFKit {
provideFont(familyName, bold, italics) {
let type = this.getFontType(bold, italics);
if (this.getFontFile(familyName, bold, italics) === null) {
throw new Error(`Font '${familyName}' in style '${type}' is not defined in the font section of the document definition.`);
throw new Error(
`Font '${familyName}' in style '${type}' is not defined in the font section of the document definition.`
);
}

this.fontCache[familyName] = this.fontCache[familyName] || {};
Expand All @@ -83,7 +97,7 @@ class PDFDocument extends PDFKit {
}

provideImage(src) {
const realImageSrc = src => {
const realImageSrc = (src) => {
let image = this.images[src];

if (!image) {
Expand All @@ -94,12 +108,12 @@ class PDFDocument extends PDFKit {
return this.virtualfs.readFileSync(image);
}

let index = image.indexOf('base64,');
let index = image.indexOf("base64,");
if (index < 0) {
return this.images[src];
}

return Buffer.from(image.substring(index + 7), 'base64');
return Buffer.from(image.substring(index + 7), "base64");
};

if (this._imageRegistry[src]) {
Expand All @@ -111,10 +125,12 @@ class PDFDocument extends PDFKit {
try {
image = this.openImage(realImageSrc(src));
if (!image) {
throw new Error('No image');
throw new Error("No image");
}
} catch (error) {
throw new Error(`Invalid image: ${error.toString()}\nImages dictionary should contain dataURL entries (or local file paths in node.js)`);
throw new Error(
`Invalid image: ${error.toString()}\nImages dictionary should contain dataURL entries (or local file paths in node.js)`
);
}

image.embed(this);
Expand All @@ -136,9 +152,9 @@ class PDFDocument extends PDFKit {
}

provideAttachment(src) {
const checkRequired = obj => {
const checkRequired = (obj) => {
if (!obj) {
throw new Error('No attachment');
throw new Error("No attachment");
}
if (!obj.src) {
throw new Error('The "src" key is required for attachments');
Expand All @@ -147,7 +163,7 @@ class PDFDocument extends PDFKit {
return obj;
};

if (typeof src === 'object') {
if (typeof src === "object") {
return checkRequired(src);
}

Expand All @@ -162,9 +178,9 @@ class PDFDocument extends PDFKit {

setOpenActionAsPrint() {
let printActionRef = this.ref({
Type: 'Action',
S: 'Named',
N: 'Print'
Type: "Action",
S: "Named",
N: "Print",
});
this._root.data.OpenAction = printActionRef;
printActionRef.end();
Expand Down
2 changes: 1 addition & 1 deletion src/TextBreaker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LineBreaker from '@foliojs-fork/linebreak';
import LineBreaker from 'linebreak';
import { isObject } from './helpers/variableType';
import StyleContextStack from './StyleContextStack';

Expand Down
63 changes: 49 additions & 14 deletions src/browser-extensions/standard-fonts/Courier.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
var fs = require('fs');
var fs = require("fs");

var fontContainer = {
vfs: {
'data/Courier.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Courier.afm', 'utf8'), encoding: 'utf8' },
'data/Courier-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Courier-Bold.afm', 'utf8'), encoding: 'utf8' },
'data/Courier-Oblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Courier-Oblique.afm', 'utf8'), encoding: 'utf8' },
'data/Courier-BoldOblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Courier-BoldOblique.afm', 'utf8'), encoding: 'utf8' }
"data/Courier.afm": {
data: fs.readFileSync(
__dirname + "/../../../node_modules/pdfkit/js/data/Courier.afm",
"utf8"
),
encoding: "utf8",
},
"data/Courier-Bold.afm": {
data: fs.readFileSync(
__dirname + "/../../../node_modules/pdfkit/js/data/Courier-Bold.afm",
"utf8"
),
encoding: "utf8",
},
"data/Courier-Oblique.afm": {
data: fs.readFileSync(
__dirname + "/../../../node_modules/pdfkit/js/data/Courier-Oblique.afm",
"utf8"
),
encoding: "utf8",
},
"data/Courier-BoldOblique.afm": {
data: fs.readFileSync(
__dirname +
"/../../../node_modules/pdfkit/js/data/Courier-BoldOblique.afm",
"utf8"
),
encoding: "utf8",
},
},
fonts: {
Courier: {
normal: 'Courier',
bold: 'Courier-Bold',
italics: 'Courier-Oblique',
bolditalics: 'Courier-BoldOblique'
}
}
normal: "Courier",
bold: "Courier-Bold",
italics: "Courier-Oblique",
bolditalics: "Courier-BoldOblique",
},
},
};

var _global = typeof window === 'object' ? window : typeof global === 'object' ? global : typeof self === 'object' ? self : this;
if (typeof _global.pdfMake !== 'undefined' && typeof _global.pdfMake.addFontContainer !== 'undefined') {
var _global =
typeof window === "object"
? window
: typeof global === "object"
? global
: typeof self === "object"
? self
: this;
if (
typeof _global.pdfMake !== "undefined" &&
typeof _global.pdfMake.addFontContainer !== "undefined"
) {
_global.pdfMake.addFontContainer(fontContainer);
}

if (typeof module !== 'undefined') {
if (typeof module !== "undefined") {
module.exports = fontContainer;
}
8 changes: 4 additions & 4 deletions src/browser-extensions/standard-fonts/Helvetica.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ var fs = require('fs');

var fontContainer = {
vfs: {
'data/Helvetica.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Helvetica.afm', 'utf8'), encoding: 'utf8' },
'data/Helvetica-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Helvetica-Bold.afm', 'utf8'), encoding: 'utf8' },
'data/Helvetica-Oblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Helvetica-Oblique.afm', 'utf8'), encoding: 'utf8' },
'data/Helvetica-BoldOblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Helvetica-BoldOblique.afm', 'utf8'), encoding: 'utf8' }
'data/Helvetica.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Helvetica.afm', 'utf8'), encoding: 'utf8' },
'data/Helvetica-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Helvetica-Bold.afm', 'utf8'), encoding: 'utf8' },
'data/Helvetica-Oblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Helvetica-Oblique.afm', 'utf8'), encoding: 'utf8' },
'data/Helvetica-BoldOblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Helvetica-BoldOblique.afm', 'utf8'), encoding: 'utf8' }
},
fonts: {
Helvetica: {
Expand Down
32 changes: 24 additions & 8 deletions src/browser-extensions/standard-fonts/Symbol.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
var fs = require('fs');
var fs = require("fs");

var fontContainer = {
vfs: {
'data/Symbol.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Symbol.afm', 'utf8'), encoding: 'utf8' }
"data/Symbol.afm": {
data: fs.readFileSync(
__dirname + "/../../../node_modules/pdfkit/js/data/Symbol.afm",
"utf8"
),
encoding: "utf8",
},
},
fonts: {
Symbol: {
normal: 'Symbol'
}
}
normal: "Symbol",
},
},
};

var _global = typeof window === 'object' ? window : typeof global === 'object' ? global : typeof self === 'object' ? self : this;
if (typeof _global.pdfMake !== 'undefined' && typeof _global.pdfMake.addFontContainer !== 'undefined') {
var _global =
typeof window === "object"
? window
: typeof global === "object"
? global
: typeof self === "object"
? self
: this;
if (
typeof _global.pdfMake !== "undefined" &&
typeof _global.pdfMake.addFontContainer !== "undefined"
) {
_global.pdfMake.addFontContainer(fontContainer);
}

if (typeof module !== 'undefined') {
if (typeof module !== "undefined") {
module.exports = fontContainer;
}
63 changes: 49 additions & 14 deletions src/browser-extensions/standard-fonts/Times.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
var fs = require('fs');
var fs = require("fs");

var fontContainer = {
vfs: {
'data/Times-Roman.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Times-Roman.afm', 'utf8'), encoding: 'utf8' },
'data/Times-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Times-Bold.afm', 'utf8'), encoding: 'utf8' },
'data/Times-Italic.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Times-Italic.afm', 'utf8'), encoding: 'utf8' },
'data/Times-BoldItalic.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/@foliojs-fork/pdfkit/js/data/Times-BoldItalic.afm', 'utf8'), encoding: 'utf8' }
"data/Times-Roman.afm": {
data: fs.readFileSync(
__dirname + "/../../../node_modules/pdfkit/js/data/Times-Roman.afm",
"utf8"
),
encoding: "utf8",
},
"data/Times-Bold.afm": {
data: fs.readFileSync(
__dirname + "/../../../node_modules/pdfkit/js/data/Times-Bold.afm",
"utf8"
),
encoding: "utf8",
},
"data/Times-Italic.afm": {
data: fs.readFileSync(
__dirname + "/../../../node_modules/pdfkit/js/data/Times-Italic.afm",
"utf8"
),
encoding: "utf8",
},
"data/Times-BoldItalic.afm": {
data: fs.readFileSync(
__dirname +
"/../../../node_modules/pdfkit/js/data/Times-BoldItalic.afm",
"utf8"
),
encoding: "utf8",
},
},
fonts: {
Times: {
normal: 'Times-Roman',
bold: 'Times-Bold',
italics: 'Times-Italic',
bolditalics: 'Times-BoldItalic'
}
}
normal: "Times-Roman",
bold: "Times-Bold",
italics: "Times-Italic",
bolditalics: "Times-BoldItalic",
},
},
};

var _global = typeof window === 'object' ? window : typeof global === 'object' ? global : typeof self === 'object' ? self : this;
if (typeof _global.pdfMake !== 'undefined' && typeof _global.pdfMake.addFontContainer !== 'undefined') {
var _global =
typeof window === "object"
? window
: typeof global === "object"
? global
: typeof self === "object"
? self
: this;
if (
typeof _global.pdfMake !== "undefined" &&
typeof _global.pdfMake.addFontContainer !== "undefined"
) {
_global.pdfMake.addFontContainer(fontContainer);
}

if (typeof module !== 'undefined') {
if (typeof module !== "undefined") {
module.exports = fontContainer;
}
Loading

0 comments on commit 05f7121

Please sign in to comment.