Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add widget page support for placeholder-plain #246

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/placeholder-plain/dist/getPageRef.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* @param {Buffer} pdfBuffer
* @param {Object} info As extracted from readRef()
* @param {Number} [pageNumber = 0] Desired page number
*/
export default function getPageRef(pdfBuffer: Buffer, info: any): string;
export default function getPageRef(pdfBuffer: Buffer, info: any, pageNumber?: number): any;
//# sourceMappingURL=getPageRef.d.ts.map
2 changes: 1 addition & 1 deletion packages/placeholder-plain/dist/getPageRef.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/placeholder-plain/dist/getPageRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = getPageRef;
var _utils = require("@signpdf/utils");
var _getPagesDictionaryRef = _interopRequireDefault(require("./getPagesDictionaryRef"));
var _findObject = _interopRequireDefault(require("./findObject"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Expand All @@ -12,14 +13,19 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
*
* @param {Buffer} pdfBuffer
* @param {Object} info As extracted from readRef()
* @param {Number} [pageNumber = 0] Desired page number
*/
function getPageRef(pdfBuffer, info) {
function getPageRef(pdfBuffer, info, pageNumber = 0) {
const pagesRef = (0, _getPagesDictionaryRef.default)(info);
const pagesDictionary = (0, _findObject.default)(pdfBuffer, info.xref, pagesRef);
const kidsPosition = pagesDictionary.indexOf('/Kids');
const kidsStart = pagesDictionary.indexOf('[', kidsPosition) + 1;
const kidsEnd = pagesDictionary.indexOf(']', kidsPosition);
const pages = pagesDictionary.slice(kidsStart, kidsEnd).toString();
const split = pages.trim().split(' ', 3);
return `${split[0]} ${split[1]} ${split[2]}`;
const pagesSplit = [];
pages.trim().split(' ').forEach((v, i) => i % 3 === 0 ? pagesSplit.push([v]) : pagesSplit[pagesSplit.length - 1].push(v));
if (pageNumber < 0 || pagesSplit.length <= pageNumber) {
throw new _utils.SignPdfError(`Failed to get reference of page "${pageNumber}".`, _utils.SignPdfError.TYPE_INPUT);
}
return pagesSplit[pageNumber].join(' ');
}
6 changes: 5 additions & 1 deletion packages/placeholder-plain/dist/plainAddPlaceholder.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function plainAddPlaceholder({ pdfBuffer, reason, contactInfo, name, location, signingTime, signatureLength, subFilter, widgetRect, appName, }: InputType): Buffer;
export function plainAddPlaceholder({ pdfBuffer, reason, contactInfo, name, location, signingTime, signatureLength, subFilter, widgetRect, widgetPage, appName, }: InputType): Buffer;
export type InputType = {
pdfBuffer: Buffer;
reason: string;
Expand All @@ -15,6 +15,10 @@ export type InputType = {
* [x1, y1, x2, y2] widget rectangle
*/
widgetRect?: number[];
/**
* Page number where the widget should be placed
*/
widgetPage?: number;
/**
* Name of the application generating the signature
*/
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/placeholder-plain/dist/plainAddPlaceholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const getAcroFormRef = slice => {
* @property {number} [signatureLength]
* @property {string} [subFilter] One of SUBFILTER_* from \@signpdf/utils
* @property {number[]} [widgetRect] [x1, y1, x2, y2] widget rectangle
* @property {number} [widgetPage] Page number where the widget should be placed
* @property {string} [appName] Name of the application generating the signature
*/

Expand All @@ -64,11 +65,12 @@ const plainAddPlaceholder = ({
signatureLength = _utils.DEFAULT_SIGNATURE_LENGTH,
subFilter = _utils.SUBFILTER_ADOBE_PKCS7_DETACHED,
widgetRect = [0, 0, 0, 0],
widgetPage = 0,
appName = undefined
}) => {
let pdf = (0, _utils.removeTrailingNewLine)(pdfBuffer);
const info = (0, _readPdf.default)(pdf);
const pageRef = (0, _getPageRef.default)(pdf, info);
const pageRef = (0, _getPageRef.default)(pdf, info, widgetPage);
const pageIndex = (0, _getIndexFromRef.default)(info.xref, pageRef);
const addedReferences = new Map();
const pdfKitMock = {
Expand Down Expand Up @@ -109,6 +111,7 @@ const plainAddPlaceholder = ({
signatureLength,
subFilter,
widgetRect,
widgetPage,
appName
});
if (!getAcroFormRef(pdf.toString())) {
Expand Down
12 changes: 9 additions & 3 deletions packages/placeholder-plain/src/getPageRef.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {SignPdfError} from '@signpdf/utils';
import getPagesDictionaryRef from './getPagesDictionaryRef';
import findObject from './findObject';

Expand All @@ -6,14 +7,19 @@ import findObject from './findObject';
*
* @param {Buffer} pdfBuffer
* @param {Object} info As extracted from readRef()
* @param {Number} [pageNumber = 0] Desired page number
*/
export default function getPageRef(pdfBuffer, info) {
export default function getPageRef(pdfBuffer, info, pageNumber = 0) {
const pagesRef = getPagesDictionaryRef(info);
const pagesDictionary = findObject(pdfBuffer, info.xref, pagesRef);
const kidsPosition = pagesDictionary.indexOf('/Kids');
const kidsStart = pagesDictionary.indexOf('[', kidsPosition) + 1;
const kidsEnd = pagesDictionary.indexOf(']', kidsPosition);
const pages = pagesDictionary.slice(kidsStart, kidsEnd).toString();
const split = pages.trim().split(' ', 3);
return `${split[0]} ${split[1]} ${split[2]}`;
const pagesSplit = [];
pages.trim().split(' ').forEach((v, i) => (i % 3 === 0 ? pagesSplit.push([v]) : pagesSplit[pagesSplit.length - 1].push(v)));
if (pageNumber < 0 || pagesSplit.length <= pageNumber) {
throw new SignPdfError(`Failed to get reference of page "${pageNumber}".`, SignPdfError.TYPE_INPUT);
}
return pagesSplit[pageNumber].join(' ');
}
5 changes: 4 additions & 1 deletion packages/placeholder-plain/src/plainAddPlaceholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const getAcroFormRef = (slice) => {
* @property {number} [signatureLength]
* @property {string} [subFilter] One of SUBFILTER_* from \@signpdf/utils
* @property {number[]} [widgetRect] [x1, y1, x2, y2] widget rectangle
* @property {number} [widgetPage] Page number where the widget should be placed
* @property {string} [appName] Name of the application generating the signature
*/

Expand All @@ -67,11 +68,12 @@ export const plainAddPlaceholder = ({
signatureLength = DEFAULT_SIGNATURE_LENGTH,
subFilter = SUBFILTER_ADOBE_PKCS7_DETACHED,
widgetRect = [0, 0, 0, 0],
widgetPage = 0,
appName = undefined,
}) => {
let pdf = removeTrailingNewLine(pdfBuffer);
const info = readPdf(pdf);
const pageRef = getPageRef(pdf, info);
const pageRef = getPageRef(pdf, info, widgetPage);
const pageIndex = getIndexFromRef(info.xref, pageRef);
const addedReferences = new Map();

Expand Down Expand Up @@ -126,6 +128,7 @@ export const plainAddPlaceholder = ({
signatureLength,
subFilter,
widgetRect,
widgetPage,
appName,
});

Expand Down
Loading