Skip to content

Commit

Permalink
Working on text signature and placing on multiple pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
zboris12 committed Aug 9, 2024
1 parent fd2b97d commit 0505fd1
Show file tree
Hide file tree
Showing 5 changed files with 583 additions and 143 deletions.
73 changes: 62 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ And I use this name to hope the merits from this application will be dedicated t
## Main features

* Sign a pdf with an invisible pkcs#7 signature.
* Sign a pdf with a visible pkcs#7 signature by drawing an image.
* Sign a pdf with a visible pkcs#7 signature by drawing an image or a text or both.
* A visible signature can be placed on multiple pages. (In the same position)
* Sign a pdf and set [DocMDP](https://github.com/zboris12/zgapdfsigner/wiki/API#note).
* Add a new signature to a pdf if it has been signed already. (An incremental update)
* Add a document timestamp from [TSA](https://github.com/zboris12/zgapdfsigner/wiki/API#note). ( :no_entry_sign:__Not__ available in web browser)
Expand Down Expand Up @@ -133,8 +134,10 @@ async function sign2(pdf, cert, pwd, imgdat, imgtyp){
w: 60, // width
h: 60, // height
},
imgData: imgdat,
imgType: imgtyp,
imgInfo: {
imgData: imgdat,
imgType: imgtyp,
},
},
};
var signer = new Zga.PdfSigner(sopt);
Expand All @@ -143,10 +146,41 @@ async function sign2(pdf, cert, pwd, imgdat, imgtyp){
}
```

Sign with a visible signature of drawing a text.
Sign with a visible signature by drawing a text.

```js
//TODO
/**
* @param {ArrayBuffer} pdf
* @param {ArrayBuffer} cert
* @param {string} pwd
* @param {string} txt
* @param {ArrayBuffer} fontdat
* @return {Promise<Blob>}
*/
async function sign3(pdf, cert, pwd, txt, fontdat){
/** @type {SignOption} */
var sopt = {
p12cert: cert,
pwd: pwd,
drawinf: {
area: {
x: 25, // left
y: 150, // top
w: 60, // width
h: 60, // height
},
textInfo: {
text: txt,
fontData: fontdat,
color: "#00f0f1",
size: 16,
},
},
};
var signer = new Zga.PdfSigner(sopt);
var u8arr = await signer.sign(pdf);
return new Blob([u8arr], {"type" : "application/pdf"});
}
```

Use it in [Google Apps Script](https://developers.google.com/apps-script)
Expand Down Expand Up @@ -198,6 +232,10 @@ async function main(){
var ps = "";
/** @type {string} */
var imgPath = m_path.join(__dirname, "_test.png");
/** @type {string} */
var txt = "I am a test string!";
/** @type {string} */
var fontPath = m_path.join(__dirname, "_test.ttf");

if(process.argv.length > 3){
pfxPath = process.argv[2];
Expand Down Expand Up @@ -226,6 +264,11 @@ async function main(){
img = m_fs.readFileSync(imgPath);
imgType = m_path.extname(imgPath).slice(1);
}
/** @type {Buffer} */
var font = null;
if(fontPath){
font = m_fs.readFileSync(fontPath);
}

/** @type {SignOption} */
var sopt = {
Expand All @@ -239,16 +282,24 @@ async function main(){
ltv: 1,
debug: true,
};
if(img){
if(img || txt){
sopt.drawinf = {
area: {
x: 25, // left
y: 150, // top
w: 60,
h: 60,
y: 50, // top
w: txt ? undefined : 60,
h: txt ? undefined : 100,
},
imgData: img,
imgType: imgType,
pageidx: "2-3", // Placed the signature on the 3rd page and the 4th page. (Indexes of pages start from 0)
imgInfo: img ? {
imgData: img,
imgType: imgType,
} : undefined,
textInfo: txt ? {
text: txt,
fontData: font,
size: 16,
} : undefined,
};
}

Expand Down
46 changes: 44 additions & 2 deletions closure/pdflib-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/
var PdfLoadOptions;

/** @const */
var fontkit = {};

/** @const */
var PDFLib = {};

Expand All @@ -23,6 +26,21 @@ PDFLib.copyStringIntoBuffer = function(str, buffer, offset){};
* @return {Uint8Array}
*/
PDFLib.toUint8Array = function(input){};
/**
* @param {string} text
* @return {string}
*/
PDFLib.cleanText = function(text){};
/**
* @param {string} text
* @return {Array<string>}
*/
PDFLib.lineSplit = function(text){};
/**
* @param {string} text
* @return {boolean}
*/
PDFLib.isNewlineChar = function(text){};

/** @constructor */
PDFLib.PDFDocument = function(){};
Expand Down Expand Up @@ -50,6 +68,10 @@ PDFLib.PDFDocument.prototype.save = function(options){};
* @returns {Array<PDFLib.PDFPage>}
*/
PDFLib.PDFDocument.prototype.getPages = function(){};
/**
* @returns {number}
*/
PDFLib.PDFDocument.prototype.getPageCount = function(){};
/**
* @param {ArrayBuffer|Uint8Array|string} png
* @returns {Promise<PDFLib.PDFImage>}
Expand Down Expand Up @@ -82,6 +104,10 @@ PDFLib.PDFDocument.prototype.embedFont = function(font, options){};
* @returns {PDFLib.PDFFont}
*/
PDFLib.PDFDocument.prototype.embedStandardFont = function(font, customName){};
/**
* @lends {fontkit} fkt
*/
PDFLib.PDFDocument.prototype.registerFontkit = function(fkt){};
/**
* @returns {Promise<number>}
*/
Expand Down Expand Up @@ -383,8 +409,23 @@ PDFLib.PDFFont = function(){};
PDFLib.PDFFont.prototype.ref;
/** @type {string} */
PDFLib.PDFFont.prototype.name;
/** @constructor */
PDFLib.StandardFonts = function(){};
/**
* @param {string} text
* @return {PDFLib.PDFHexString }
*/
PDFLib.PDFFont.prototype.encodeText = function(text){};
/**
* @param {number} size
* @param {Object<string, boolean>=} options
* @return {number}
*/
PDFLib.PDFFont.prototype.heightAtSize = function(size, options){};
/**
* @param {string} text
* @param {number} size
* @return {number}
*/
PDFLib.PDFFont.prototype.widthOfTextAtSize = function(text, size){};

PDFLib.RotationTypes = {};
/** @type {string} */
Expand Down Expand Up @@ -416,6 +457,7 @@ PDFLib.PDFOperator = function(){};
* rotate: (PDFLib.Rotation|undefined),
* xSkew: (PDFLib.Rotation|undefined),
* ySkew: (PDFLib.Rotation|undefined),
* graphicsState: (string|undefined),
* }}
*/
var PdfDrawimgOption;
Expand Down
60 changes: 55 additions & 5 deletions closure/zb-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,74 @@
var TsaServiceInfo;
/**
* the base point of x, y is top left corner.
* wDraw, hDraw: Only for internal process.
* @typedef
* {{
* x: number,
* y: number,
* w: number,
* h: number,
* w: (number|undefined),
* h: (number|undefined),
* wDraw: (number|undefined),
* hDraw: (number|undefined),
* }}
*/
var SignAreaInfo;
/**
* fontData: default: StandardFonts.Helvetica
* color: A Hex string of color. default: #000
* opacity: valid value is from 0 to 1. default: 1 // Not implemented
* blendMode: https://pdf-lib.js.org/docs/api/enums/blendmode // Not implemented
* lineHeight: default is the height of the font at the given size
* xOffset: An offset from SignAreaInfo's x
* yOffset: An offset from SignAreaInfo's y
* align: Text alignment: 0 left, 1 center, 2 right. default: 0
* noBreaks: A regular expression string that indicates which characters should not be used to break a word. default: [A-Za-z0-9]
*
* @typedef
* {{
* text: string,
* fontData: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
* color: (string|undefined),
* opacity: (number|undefined),
* blendMode: (string|undefined),
* lineHeight: (number|undefined),
* size: number,
* xOffset: (number|undefined),
* yOffset: (number|undefined),
* wMax: (number|undefined),
* align: (number|undefined),
* noBreaks: (string|undefined),
* }}
*/
var SignTextInfo;
/**
* opacity: valid value is from 0 to 1. default: 1 // Not implemented
* blendMode: https://pdf-lib.js.org/docs/api/enums/blendmode // Not implemented
*
* @typedef
* {{
* imgData: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
* imgType: (string|undefined),
* opacity: (number|undefined),
* blendMode: (string|undefined),
* }}
*/
var SignImageInfo;
/**
* The signature can be placed in the same position on multiple pages, but all pages must have the same size and rotation angle.
* pageidx: Can be a string to indicate placing the signature on multiple pages.
* For example: A pdf contains 17 pages and specify "-3,5-7,9,12,15-" means [0,1,2,3,5,6,7,9,12,15,16]
* imgData, imgType: Deprecated, use imgInfo instead.
* img, font: Only for internal process.
*
* @typedef
* {{
* area: SignAreaInfo,
* pageidx: (number|undefined),
* pageidx: (number|string|undefined),
* imgData: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
* imgType: (string|undefined),
* text: (string|undefined),
* fontData: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
* imgInfo: (SignImageInfo|undefined),
* textInfo: (SignTextInfo|undefined),
* img: (PDFLib.PDFImage|undefined),
* font: (PDFLib.PDFFont|undefined),
* }}
Expand Down
Loading

0 comments on commit 0505fd1

Please sign in to comment.