Skip to content

Commit

Permalink
Improved to support font subsetting and changed to use pdf-fontkit.
Browse files Browse the repository at this point in the history
  • Loading branch information
zboris12 committed Aug 19, 2024
1 parent 4def406 commit 9706bfe
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 344 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ signing with a timestamp from [TSA](https://github.com/zboris12/zgapdfsigner/wik
* [pdf-lib](https://pdf-lib.js.org/)
* [node-forge](https://github.com/digitalbazaar/forge)
* [follow-redirects](https://github.com/follow-redirects/follow-redirects)
* [pako](https://github.com/nodeca/pako) - For drawing text
* [pdf-fontkit](https://github.com/znacloud/pdf-fontkit) - For drawing text

## How to use this tool

Expand All @@ -53,9 +55,10 @@ Just import the dependencies and this tool.
```
When drawing text for signature, importing fontkit and pako library is necessary.
```html
<script src="https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/pdf-fontkit@1.8.9/dist/fontkit.umd.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/[email protected]/dist/pako_inflate.min.js" type="text/javascript"></script>
```
Thanks to [znacloud](https://github.com/znacloud/pdf-fontkit) for fixing the font subsetting issue in [@pdf-lib/fontkit](https://github.com/Hopding/fontkit).

### [Google Apps Script](https://developers.google.com/apps-script)
Load the dependencies and this tool.
Expand All @@ -70,7 +73,7 @@ var window = globalThis;
// Load pdf-lib
eval(UrlFetchApp.fetch("https://unpkg.com/[email protected]/dist/pdf-lib.min.js").getContentText());
// It is necessary for drawing text for signature.
eval(UrlFetchApp.fetch("https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js").getContentText());
eval(UrlFetchApp.fetch("https://unpkg.com/pdf-fontkit@1.8.9/dist/fontkit.umd.min.js").getContentText());
// Load pako, It is necessary for drawing text for signature.
eval(UrlFetchApp.fetch("https://unpkg.com/[email protected]/dist/pako_inflate.min.js").getContentText());
// Load node-forge
Expand Down
1 change: 1 addition & 0 deletions closure/zb-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var SignAreaInfo;
* {{
* text: string,
* fontData: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
* subset: (boolean|undefined),
* color: (string|undefined),
* opacity: (number|undefined),
* blendMode: (string|undefined),
Expand Down
3 changes: 2 additions & 1 deletion lib/zganode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type SignAreaInfo = {
export type SignTextInfo = {
text: string,
fontData?: Array<number> | Uint8Array | ArrayBuffer | PDFLib.StandardFonts;
subset?: boolean;
color?: string;
opacity?: number;
blendMode?: string;
Expand Down Expand Up @@ -112,5 +113,5 @@ export declare class TsaFetcher {
export declare class PdfFonts {
private constructor();
static from(pdfdoc: PDFLib.PDFDocument): Promise<PdfFonts>;
getEmbeddedFont(fontData?: Array<number> | Uint8Array | ArrayBuffer | PDFLib.StandardFonts): Promise<PDFLib.PDFFont>;
getEmbeddedFont(fontData?: Array<number> | Uint8Array | ArrayBuffer | PDFLib.StandardFonts, subset?: boolean): Promise<PDFLib.PDFFont>;
}
3 changes: 2 additions & 1 deletion lib/zganode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const m_h = {
const z = require("./zgaindex.js");
z.forge = require("node-forge");
z.PDFLib = require("pdf-lib");
z.fontkit = require("@pdf-lib/fontkit");
// z.fontkit = require("@pdf-lib/fontkit");
z.fontkit = require("pdf-fontkit");
z.pako = require("pako");
/**
* @param {string} url
Expand Down
28 changes: 17 additions & 11 deletions lib/zgapdfsigner.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ z.PdfSigner = class{

if(_this.opt.drawinf && _this.opt.drawinf.textInfo && !_this.opt.drawinf.font){
_this.fonts = await z.PdfFonts.from(pdfdoc);
_this.opt.drawinf.font = await _this.fonts.getEmbeddedFont(_this.opt.drawinf.textInfo.fontData);
_this.opt.drawinf.font = await _this.fonts.getEmbeddedFont(_this.opt.drawinf.textInfo.fontData, _this.opt.drawinf.textInfo.subset);
}

/** @type {forge_cert} */
Expand Down Expand Up @@ -1910,12 +1910,16 @@ z.PdfFonts = class{
if(fltr == "/FlateDecode"){
fdat = pako.inflate(fdat);
}
/** @type {PDFLib.CustomFontEmbedder} */
var emdr = await PDFLib.CustomFontEmbedder.for(fontkit, fdat);
fonts.push({
font: PDFLib.PDFFont.of(poe[0], pdfdoc, emdr),
data: fdat,
});
try{
/** @type {PDFLib.CustomFontEmbedder} */
var emdr = await PDFLib.CustomFontEmbedder.for(fontkit, fdat);
fonts.push({
font: PDFLib.PDFFont.of(poe[0], pdfdoc, emdr),
data: fdat,
});
}catch(ex){
z.log(fntnm, ex.message);
}
}
}
}
Expand All @@ -1929,9 +1933,10 @@ z.PdfFonts = class{
/**
* @public
* @param {Array<number>|Uint8Array|ArrayBuffer|string|undefined} fontData
* @param {boolean=} subset
* @return {Promise<PDFLib.PDFFont>}
*/
async getEmbeddedFont(fontData){
async getEmbeddedFont(fontData, subset){
if(!fontData){
if(this.fonts.length){
z.log("Use existing default font.", this.fonts[0].font.name);
Expand All @@ -1946,7 +1951,7 @@ z.PdfFonts = class{
}else{
/** @type {Uint8Array} */
var u8dat = (fontData instanceof Uint8Array) ? fontData : new Uint8Array(fontData);
return await this.getCustomFont(u8dat);
return await this.getCustomFont(u8dat, subset || false);
}
}

Expand All @@ -1972,9 +1977,10 @@ z.PdfFonts = class{
/**
* @private
* @param {Uint8Array} fontData
* @param {boolean} subset
* @return {Promise<PDFLib.PDFFont>}
*/
async getCustomFont(fontData){
async getCustomFont(fontData, subset){
/** @type {number} */
var i = 0;
while(i < this.fonts.length){
Expand All @@ -1987,7 +1993,7 @@ z.PdfFonts = class{
}
}
this.doc.registerFontkit(fontkit);
return await this.doc.embedFont(fontData);
return await this.doc.embedFont(fontData, {subset});
}
/**
* @private
Expand Down
Loading

0 comments on commit 9706bfe

Please sign in to comment.