Skip to content

Commit

Permalink
Improved to reuse the font if it is already embedded in the PDF.
Browse files Browse the repository at this point in the history
  • Loading branch information
zboris12 committed Aug 18, 2024
1 parent 1f92ca6 commit 4def406
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 37 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ Just import the dependencies and this tool.
<script src="https://unpkg.com/node-forge@1.3.1/dist/forge.min.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/zgapdfsigner/dist/zgapdfsigner.min.js" type="text/javascript"></script>
```
When drawing text by non-standard font, importing the fontkit library is necessary.
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/pako@1.0.11/dist/pako_inflate.min.js" type="text/javascript"></script>
```

### [Google Apps Script](https://developers.google.com/apps-script)
Expand All @@ -68,8 +69,10 @@ function setTimeout(func, sleep){
var window = globalThis;
// Load pdf-lib
eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js").getContentText());
// It is necessary for drawing text by non-standard font.
// It is necessary for drawing text for signature.
eval(UrlFetchApp.fetch("https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js").getContentText());
// Load pako, It is necessary for drawing text for signature.
eval(UrlFetchApp.fetch("https://unpkg.com/pako@1.0.11/dist/pako_inflate.min.js").getContentText());
// Load node-forge
eval(UrlFetchApp.fetch("https://unpkg.com/node-forge@1.3.1/dist/forge.min.js").getContentText());
// Load ZgaPdfSigner
Expand Down
63 changes: 61 additions & 2 deletions closure/pdflib-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@
var PdfLoadOptions;

/** @const */
var fontkit = {};
var pako = {};
/**
* @param {Uint8Array} input
* @return {Uint8Array}
*/
pako.inflate = function(input){};

/** @constructor */
var Fontkit = function(){};
/** @const {Fontkit} */
var fontkit;

/** @const */
var PDFLib = {};
Expand Down Expand Up @@ -41,6 +51,11 @@ PDFLib.lineSplit = function(text){};
* @return {boolean}
*/
PDFLib.isNewlineChar = function(text){};
/**
* @param {string} fntnm
* @return {boolean}
*/
PDFLib.isStandardFont = function(fntnm){};

/** @constructor */
PDFLib.PDFDocument = function(){};
Expand Down Expand Up @@ -105,7 +120,7 @@ PDFLib.PDFDocument.prototype.embedFont = function(font, options){};
*/
PDFLib.PDFDocument.prototype.embedStandardFont = function(font, customName){};
/**
* @lends {fontkit} fkt
* @param {Fontkit} fkt
*/
PDFLib.PDFDocument.prototype.registerFontkit = function(fkt){};
/**
Expand Down Expand Up @@ -316,6 +331,8 @@ PDFLib.PDFName.Annots;
* @return {PDFLib.PDFName}
*/
PDFLib.PDFName.of = function(value){};
/** @return {string} */
PDFLib.PDFName.prototype.asString = function(){};
/** @type {string} */
PDFLib.PDFName.prototype.encodedName;
/** @type {number} */
Expand All @@ -338,10 +355,20 @@ PDFLib.PDFArray.prototype.push = function(object){};
* @return {PDFLib.PDFObject}
*/
PDFLib.PDFArray.prototype.get = function(idx){};
/**
* @return {number}
*/
PDFLib.PDFArray.prototype.size = function(){};
/**
* @return {Array<PDFLib.PDFObject>}
*/
PDFLib.PDFArray.prototype.asArray = function(){};
/**
* @param {number} idx
* @param {*} typ
* @return {PDFLib.PDFObject}
*/
PDFLib.PDFArray.prototype.lookupMaybe = function(idx, typ){};

/**
* @constructor
Expand Down Expand Up @@ -403,8 +430,34 @@ PDFLib.PDFImage.prototype.size = function(){};
/** @type {PDFLib.PDFRef} */
PDFLib.PDFImage.prototype.ref;

/** @constructor */
PDFLib.StandardFontEmbedder = function(){};
/**
* @param {string} fontName
* @param {string=} customName
* @return {PDFLib.StandardFontEmbedder}
*/
PDFLib.StandardFontEmbedder.for = function(fontName, customName){};

/** @constructor */
PDFLib.CustomFontEmbedder = function(){};
/**
* @param {Fontkit} fontkit
* @param {Uint8Array} fontData
* @param {string=} customName
* @return {PDFLib.CustomFontEmbedder}
*/
PDFLib.CustomFontEmbedder.for = function(fontkit, fontData, customName){};

/** @constructor */
PDFLib.PDFFont = function(){};
/**
* @param {PDFLib.PDFRef} ref
* @param {PDFLib.PDFDocument} doc
* @param {PDFLib.StandardFontEmbedder|PDFLib.CustomFontEmbedder} embedder
* @return {PDFLib.PDFFont}
*/
PDFLib.PDFFont.of = function(ref, doc, embedder){};
/** @type {PDFLib.PDFRef} */
PDFLib.PDFFont.prototype.ref;
/** @type {string} */
Expand Down Expand Up @@ -540,6 +593,12 @@ PDFLib.PDFContentStream.of = function(dict, operators, encode){};
* @extends {PDFLib.PDFStream}
*/
PDFLib.PDFRawStream = function(){};
/** @type {PDFLib.PDFDict} */
PDFLib.PDFRawStream.prototype.dict;
/**
* @return {Uint8Array}
*/
PDFLib.PDFRawStream.prototype.getContents = function(){};

/**
* @constructor
Expand Down
6 changes: 3 additions & 3 deletions lib/zgaindex.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ function genZga(){
const z = {};

/**
* @param {string} msg
* @param {...string} msg
*/
z.log = function(msg){
z.log = function(...msg){
if(z.debug){
console.log(msg);
console.log(...msg);
}
};

Expand Down
7 changes: 6 additions & 1 deletion lib/zganode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type SignAreaInfo = {
};
export type SignTextInfo = {
text: string,
fontData?: Array<number> | Uint8Array | ArrayBuffer | string;
fontData?: Array<number> | Uint8Array | ArrayBuffer | PDFLib.StandardFonts;
color?: string;
opacity?: number;
blendMode?: string;
Expand Down Expand Up @@ -109,3 +109,8 @@ export declare class TsaFetcher {
getToken(forP7?: boolean): forge.asn1.Asn1;
queryTsa(data?: string): Promise<string>;
}
export declare class PdfFonts {
private constructor();
static from(pdfdoc: PDFLib.PDFDocument): Promise<PdfFonts>;
getEmbeddedFont(fontData?: Array<number> | Uint8Array | ArrayBuffer | PDFLib.StandardFonts): Promise<PDFLib.PDFFont>;
}
1 change: 1 addition & 0 deletions lib/zganode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const z = require("./zgaindex.js");
z.forge = require("node-forge");
z.PDFLib = require("pdf-lib");
z.fontkit = require("@pdf-lib/fontkit");
z.pako = require("pako");
/**
* @param {string} url
* @param {UrlFetchParams} params
Expand Down
Loading

0 comments on commit 4def406

Please sign in to comment.