From 6a15193d7e96b8a1decd18dc6b7aec9795a04bfe Mon Sep 17 00:00:00 2001 From: prafullnavkar-nxg Date: Wed, 25 Oct 2023 15:57:26 +0530 Subject: [PATCH] minify all files required for signpdf --- apps/OpenSign/src/App.js | 2 +- apps/OpenSign/src/constant/appinfo.js | 2 +- .../cloud/parsefunction/pdf/PDF.min.js | 2 +- .../cloud/parsefunction/pdf/SignPDF.cjs | 31 -- .../cloud/parsefunction/pdf/SignPDF.min.cjs | 1 + .../pdf/customSignPdf/PDFAbstractReference.js | 18 - .../customSignPdf/PDFAbstractReference.min.js | 1 + .../pdf/customSignPdf/PDFKitReferenceMock.js | 17 - .../customSignPdf/PDFKitReferenceMock.min.js | 1 + .../pdf/customSignPdf/SignPdfError.js | 19 - .../pdf/customSignPdf/SignPdfError.min.js | 1 + .../parsefunction/pdf/customSignPdf/const.js | 6 - .../pdf/customSignPdf/const.min.js | 1 + .../createBufferPageWithAnnotation.js | 40 -- .../createBufferPageWithAnnotation.min.js | 3 + .../createBufferRootWithAcroform.js | 15 - .../createBufferRootWithAcroform.min.js | 3 + .../pdf/customSignPdf/createBufferTrailer.js | 28 -- .../customSignPdf/createBufferTrailer.min.js | 8 + .../pdf/customSignPdf/findObject.js | 21 - .../pdf/customSignPdf/findObject.min.js | 1 + .../pdf/customSignPdf/getIndexFromRef.js | 21 - .../pdf/customSignPdf/getIndexFromRef.min.js | 1 + .../pdf/customSignPdf/getPageRef.js | 41 -- .../pdf/customSignPdf/getPageRef.min.js | 1 + .../customSignPdf/getPagesDictionaryRef.js | 18 - .../getPagesDictionaryRef.min.js | 1 + .../pdf/customSignPdf/pdfObject.js | 131 ----- .../pdf/customSignPdf/pdfObject.min.js | 1 + .../pdf/customSignPdf/pdfkitAddPlaceholder.js | 449 ------------------ .../customSignPdf/pdfkitAddPlaceholder.min.js | 28 ++ .../pdf/customSignPdf/plainplaceholder.js | 138 ------ .../pdf/customSignPdf/plainplaceholder.min.js | 2 + .../pdf/customSignPdf/readPdf.js | 54 --- .../pdf/customSignPdf/readPdf.min.js | 1 + .../pdf/customSignPdf/readRefTable.js | 118 ----- .../pdf/customSignPdf/readRefTable.min.js | 1 + .../customSignPdf/removeTrailingNewLine.js | 42 -- .../removeTrailingNewLine.min.js | 1 + .../pdf/customSignPdf/xrefToRefMap.js | 48 -- .../pdf/customSignPdf/xrefToRefMap.min.js | 1 + 41 files changed, 61 insertions(+), 1258 deletions(-) delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/SignPDF.cjs create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/SignPDF.min.cjs delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/PDFAbstractReference.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/PDFAbstractReference.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/PDFKitReferenceMock.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/PDFKitReferenceMock.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/SignPdfError.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/SignPdfError.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/const.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/const.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferPageWithAnnotation.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferPageWithAnnotation.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferRootWithAcroform.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferRootWithAcroform.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferTrailer.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferTrailer.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/findObject.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/findObject.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getIndexFromRef.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getIndexFromRef.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPageRef.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPageRef.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPagesDictionaryRef.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPagesDictionaryRef.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfObject.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfObject.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfkitAddPlaceholder.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfkitAddPlaceholder.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/plainplaceholder.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/plainplaceholder.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readPdf.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readPdf.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readRefTable.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readRefTable.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/removeTrailingNewLine.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/removeTrailingNewLine.min.js delete mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/xrefToRefMap.js create mode 100644 apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/xrefToRefMap.min.js diff --git a/apps/OpenSign/src/App.js b/apps/OpenSign/src/App.js index 2d42dbe39..76ef93ee9 100644 --- a/apps/OpenSign/src/App.js +++ b/apps/OpenSign/src/App.js @@ -23,7 +23,7 @@ function App() { const handleCredentials = () => { const appId = process.env.REACT_APP_APPID; - const baseurl = process.env.REACT_APP_SEVERURL; + const baseurl = process.env.REACT_APP_SERVERURL; const appName = "contracts"; try { localStorage.setItem("BaseUrl12", `${baseurl}/`); diff --git a/apps/OpenSign/src/constant/appinfo.js b/apps/OpenSign/src/constant/appinfo.js index bdd34c288..f0f306a6b 100644 --- a/apps/OpenSign/src/constant/appinfo.js +++ b/apps/OpenSign/src/constant/appinfo.js @@ -3,7 +3,7 @@ export const appInfo = { appTitle: "contracts", applogo: logo, appname: "contracts", - baseurl: process.env.REACT_APP_SEVERURL, + baseurl: process.env.REACT_APP_SERVERURL, defaultRole: "contracts_User", fbAppId: process.env.REACT_APP_FBAPPID ? `${process.env.REACT_APP_FBAPPID}` diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/PDF.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/PDF.min.js index 208035f83..1134d8fae 100644 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/PDF.min.js +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/PDF.min.js @@ -1 +1 @@ -import SignPDF from"./SignPDF.cjs";import fs from"node:fs";import axios from"axios";import FormData from"form-data";import plainplaceholder from"./customSignPdf/plainplaceholder.js";import{plainAddPlaceholder}from"node-signpdf/dist/helpers/index.js";const serverUrl=process.env.SERVER_URL,APPID=process.env.APP_ID,masterKEY=process.env.MASTER_KEY;async function uploadFile(a){try{var e=new FormData,t=(e.append("file",fs.createReadStream(a)),{"content-type":"multipart/form-data","X-Parse-Application-Id":process.env.APP_ID}),s=process.env.SERVER_URL.slice(0,-4)+"/file_upload";return(await axios.post(s,e,{headers:t})).data}catch(e){console.log("err ",e),fs.unlinkSync(a)}}async function updateDoc(t,s,i,r,n,o){try{var d={UserPtr:{__type:"Pointer",className:o,objectId:i},SignedUrl:s,Activity:"Signed",ipAddress:r};let e;var l=(e=n.AuditTrail&&0"Signed"===e.Activity);let a=!1;!(n.Signers&&0

Document Copy

A copy of the document "+s+" Standard is attached to this email. Kindly download the document from the attachment.

This is an automated email from Open Sign. For any queries regarding this email, please contact the sender "+t.Mail+" directly. If you think this email is inappropriate or spam, you may file a complaint with Open Sign here.

"};await axios.post(serverUrl+"/functions/sendmailv3",a,{headers:{"Content-Type":"application/json","X-Parse-Application-Id":APPID,"X-Parse-Master-Key":masterKEY}})}async function sendCompletedMail(e){var a=e.url,t=e.sender,s=e.pdfName,a={url:a,from:"Open sign",recipient:e.receiver,subject:`Document ${s} has beeen signed by all parties`,pdfName:s,html:"

Document sign successfully

All parties have successfully signed the document"+s+". Kindly download the document from the attachment.

This is an automated email from Open Sign. For any queries regarding this email, please contact the sender "+t.Mail+" directly. If you think this email is inappropriate or spam, you may file a complaint with Open Sign here.

"};await axios.post(serverUrl+"/functions/sendmailv3",a,{headers:{"Content-Type":"application/json","X-Parse-Application-Id":APPID,"X-Parse-Master-Key":masterKEY}})}async function PDF(s,i){try{var r=s.params.sign,e=s.params.docId,n=await axios.get(serverUrl+"/classes/contracts_Document/"+e+"?include=ExtUserPtr",{headers:{"Content-Type":"application/json","X-Parse-Application-Id":APPID,"X-Parse-Session-Token":s.headers.sessiontoken}}),o=await axios.get(serverUrl+"/users/me",{headers:{"X-Parse-Application-Id":APPID,"X-Parse-Session-Token":s.headers.sessiontoken}});if(!o.data||!o.data.objectId)return{status:"error",message:"this user not allowed!"};{var d=JSON.stringify({UserId:{__type:"Pointer",className:"_User",objectId:o.data.objectId}});let a,t;var l=await axios.get(serverUrl+"/classes/contracts_Users?where="+d,{headers:{"X-Parse-Application-Id":APPID,"X-Parse-Session-Token":s.headers.sessiontoken}}),p=(t=l.data&&0",location:"test location",signatureLength:1e4,sign:r}):plainAddPlaceholder({pdfBuffer:e,reason:"Digitally signed by Open sign for "+p+" <"+c+">",location:"test location",signatureLength:1e4});var u=await new SignPDF(e,g).signPDF(),f=`./exports/exported_file_${Math.floor(5e3*Math.random())}.pdf`,h=(fs.writeFileSync(f,u),await uploadFile(f));if(h&&h.imageUrl){const i=await updateDoc(s.params.docId,h.imageUrl,a.data.results[0].objectId,s.headers["x-real-ip"],n.data,t);return sendMail({url:h.imageUrl,sender:{Mail:n.data.ExtUserPtr.Email,Name:n.data.ExtUserPtr.Name},pdfName:n.data.Name,receiver:c}),i&&i.isCompleted&&sendCompletedMail({url:h.imageUrl,sender:{Mail:n.data.ExtUserPtr.Email,Name:"Open sign"},pdfName:n.data.Name,receiver:n.data.ExtUserPtr.Email}),fs.unlinkSync(f),console.log("New Signed PDF created called: "+f),"success"===i.message?{status:"success",data:h.imageUrl}:{status:"error",message:"please provide required parameters!"}}}}}catch(e){return console.log("Err ",e),"ERR_BAD_REQUEST"===e.code?{status:"error",message:"Invalid session token!"}:{status:"error",message:"Encrypted files are currently not supported!"}}}export default PDF; \ No newline at end of file +import SignPDF from"./SignPDF.min.cjs";import fs from"node:fs";import axios from"axios";import FormData from"form-data";import plainplaceholder from"./customSignPdf/plainplaceholder.min.js";import{plainAddPlaceholder}from"node-signpdf/dist/helpers/index.js";const serverUrl=process.env.SERVER_URL,APPID=process.env.APP_ID,masterKEY=process.env.MASTER_KEY;async function uploadFile(a){try{var e=new FormData,t=(e.append("file",fs.createReadStream(a)),{"content-type":"multipart/form-data","X-Parse-Application-Id":process.env.APP_ID}),s=process.env.SERVER_URL.slice(0,-4)+"/file_upload";return(await axios.post(s,e,{headers:t})).data}catch(e){console.log("err ",e),fs.unlinkSync(a)}}async function updateDoc(t,s,i,r,n,o){try{var d={UserPtr:{__type:"Pointer",className:o,objectId:i},SignedUrl:s,Activity:"Signed",ipAddress:r};let e;var l=(e=n.AuditTrail&&0"Signed"===e.Activity);let a=!1;!(n.Signers&&0

Document Copy

A copy of the document "+s+" Standard is attached to this email. Kindly download the document from the attachment.

This is an automated email from Open Sign. For any queries regarding this email, please contact the sender "+t.Mail+" directly. If you think this email is inappropriate or spam, you may file a complaint with Open Sign here.

"};await axios.post(serverUrl+"/functions/sendmailv3",a,{headers:{"Content-Type":"application/json","X-Parse-Application-Id":APPID,"X-Parse-Master-Key":masterKEY}})}async function sendCompletedMail(e){var a=e.url,t=e.sender,s=e.pdfName,a={url:a,from:"Open sign",recipient:e.receiver,subject:`Document ${s} has beeen signed by all parties`,pdfName:s,html:"

Document sign successfully

All parties have successfully signed the document"+s+". Kindly download the document from the attachment.

This is an automated email from Open Sign. For any queries regarding this email, please contact the sender "+t.Mail+" directly. If you think this email is inappropriate or spam, you may file a complaint with Open Sign here.

"};await axios.post(serverUrl+"/functions/sendmailv3",a,{headers:{"Content-Type":"application/json","X-Parse-Application-Id":APPID,"X-Parse-Master-Key":masterKEY}})}async function PDF(s,i){try{var r=s.params.sign,e=s.params.docId,n=await axios.get(serverUrl+"/classes/contracts_Document/"+e+"?include=ExtUserPtr",{headers:{"Content-Type":"application/json","X-Parse-Application-Id":APPID,"X-Parse-Session-Token":s.headers.sessiontoken}}),o=await axios.get(serverUrl+"/users/me",{headers:{"X-Parse-Application-Id":APPID,"X-Parse-Session-Token":s.headers.sessiontoken}});if(!o.data||!o.data.objectId)return{status:"error",message:"this user not allowed!"};{var d=JSON.stringify({UserId:{__type:"Pointer",className:"_User",objectId:o.data.objectId}});let a,t;var l=await axios.get(serverUrl+"/classes/contracts_Users?where="+d,{headers:{"X-Parse-Application-Id":APPID,"X-Parse-Session-Token":s.headers.sessiontoken}}),p=(t=l.data&&0",location:"test location",signatureLength:1e4,sign:r}):plainAddPlaceholder({pdfBuffer:e,reason:"Digitally signed by Open sign for "+p+" <"+c+">",location:"test location",signatureLength:1e4});var g=await new SignPDF(e,m).signPDF(),u=`./exports/exported_file_${Math.floor(5e3*Math.random())}.pdf`,f=(fs.writeFileSync(u,g),await uploadFile(u));if(f&&f.imageUrl){const i=await updateDoc(s.params.docId,f.imageUrl,a.data.results[0].objectId,s.headers["x-real-ip"],n.data,t);return sendMail({url:f.imageUrl,sender:{Mail:n.data.ExtUserPtr.Email,Name:n.data.ExtUserPtr.Name},pdfName:n.data.Name,receiver:c}),i&&i.isCompleted&&sendCompletedMail({url:f.imageUrl,sender:{Mail:n.data.ExtUserPtr.Email,Name:"Open sign"},pdfName:n.data.Name,receiver:n.data.ExtUserPtr.Email}),fs.unlinkSync(u),console.log("New Signed PDF created called: "+u),"success"===i.message?{status:"success",data:f.imageUrl}:{status:"error",message:"please provide required parameters!"}}}}}catch(e){return console.log("Err ",e),"ERR_BAD_REQUEST"===e.code?{status:"error",message:"Invalid session token!"}:{status:"error",message:"Encrypted files are currently not supported!"}}}export default PDF; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/SignPDF.cjs b/apps/OpenSignServer/cloud/parsefunction/pdf/SignPDF.cjs deleted file mode 100644 index ea661be5e..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/SignPDF.cjs +++ /dev/null @@ -1,31 +0,0 @@ -const signer = require("node-signpdf").default; - -class SignPDF { - constructor(pdfBuffer, certBuffer) { - this.pdfDoc = pdfBuffer; - this.certificate = certBuffer; - } - /** - * @return Promise - */ - async signPDF() { - let newPDF = signer.sign(this.pdfDoc, this.certificate, { - passphrase: "emudhra", - }); - return newPDF; - } - - /** - * @param {Uint8Array} unit8 - */ - static unit8ToBuffer(unit8) { - let buf = Buffer.alloc(unit8.byteLength); - const view = new Uint8Array(unit8); - - for (let i = 0; i < buf.length; ++i) { - buf[i] = view[i]; - } - return buf; - } -} -module.exports = SignPDF; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/SignPDF.min.cjs b/apps/OpenSignServer/cloud/parsefunction/pdf/SignPDF.min.cjs new file mode 100644 index 000000000..509e2dd53 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/SignPDF.min.cjs @@ -0,0 +1 @@ +const signer=require("node-signpdf").default;class SignPDF{constructor(e,t){this.pdfDoc=e,this.certificate=t}async signPDF(){return signer.sign(this.pdfDoc,this.certificate,{passphrase:"emudhra"})}static unit8ToBuffer(e){var t=Buffer.alloc(e.byteLength),r=new Uint8Array(e);for(let e=0;e { - const pagesDictionary = findObject(pdf, info.xref, pagesRef).toString(); - - // Extend page dictionary with newly created annotations - let annotsStart; let annotsEnd; let - annots; - annotsStart = pagesDictionary.indexOf('/Annots'); - if (annotsStart > -1) { - annotsEnd = pagesDictionary.indexOf(']', annotsStart); - annots = pagesDictionary.substr(annotsStart, annotsEnd + 1 - annotsStart); - annots = annots.substr(0, annots.length - 1); // remove the trailing ] - } else { - annotsStart = pagesDictionary.length; - annotsEnd = pagesDictionary.length; - annots = '/Annots ['; - } - - const pagesDictionaryIndex = getIndexFromRef(info.xref, pagesRef); - const widgetValue = widget.toString(); - - annots = `${annots} ${widgetValue}]`; // add the trailing ] back - - const preAnnots = pagesDictionary.substr(0, annotsStart); - let postAnnots = ''; - if (pagesDictionary.length > annotsEnd) { - postAnnots = pagesDictionary.substr(annotsEnd + 1); - } - - return Buffer.concat([ - Buffer.from(`${pagesDictionaryIndex} 0 obj\n`), - Buffer.from('<<\n'), - Buffer.from(`${preAnnots + annots + postAnnots}\n`), - Buffer.from('\n>>\nendobj\n'), - ]); -}; - -export default createBufferPageWithAnnotation; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferPageWithAnnotation.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferPageWithAnnotation.min.js new file mode 100644 index 000000000..ea4856403 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferPageWithAnnotation.min.js @@ -0,0 +1,3 @@ +import findObject from"./findObject.min.js";import getIndexFromRef from"./getIndexFromRef.min.js";const createBufferPageWithAnnotation=(e,t,n,f)=>{e=findObject(e,t.xref,n).toString();let r,o,i;r=e.indexOf("/Annots"),i=-1o&&(m=e.substr(o+1)),Buffer.concat([Buffer.from(t+` 0 obj +`),Buffer.from("<<\n"),Buffer.from(f+i+m+` +`),Buffer.from("\n>>\nendobj\n")])};export default createBufferPageWithAnnotation; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferRootWithAcroform.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferRootWithAcroform.js deleted file mode 100644 index 2ee1af794..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferRootWithAcroform.js +++ /dev/null @@ -1,15 +0,0 @@ -import getIndexFromRef from './getIndexFromRef.js'; - -const createBufferRootWithAcroform = (pdf, info, form) => { - const rootIndex = getIndexFromRef(info.xref, info.rootRef); - - return Buffer.concat([ - Buffer.from(`${rootIndex} 0 obj\n`), - Buffer.from('<<\n'), - Buffer.from(`${info.root}\n`), - Buffer.from(`/AcroForm ${form}`), - Buffer.from('\n>>\nendobj\n'), - ]); -}; - -export default createBufferRootWithAcroform; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferRootWithAcroform.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferRootWithAcroform.min.js new file mode 100644 index 000000000..9783e9b51 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferRootWithAcroform.min.js @@ -0,0 +1,3 @@ +import getIndexFromRef from"./getIndexFromRef.min.js";const createBufferRootWithAcroform=(r,f,o)=>{var e=getIndexFromRef(f.xref,f.rootRef);return Buffer.concat([Buffer.from(e+` 0 obj +`),Buffer.from("<<\n"),Buffer.from(f.root+` +`),Buffer.from("/AcroForm "+o),Buffer.from("\n>>\nendobj\n")])};export default createBufferRootWithAcroform; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferTrailer.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferTrailer.js deleted file mode 100644 index bb45e714e..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferTrailer.js +++ /dev/null @@ -1,28 +0,0 @@ -const createBufferTrailer = (pdf, info, addedReferences) => { - let rows = []; - rows[0] = '0000000000 65535 f '; // info.xref.tableRows[0]; - - addedReferences.forEach((offset, index) => { - const paddedOffset = (`0000000000${offset}`).slice(-10); - rows[index + 1] = `${index} 1\n${paddedOffset} 00000 n `; - }); - rows = rows.filter((row) => row !== undefined); - - return Buffer.concat([ - Buffer.from('xref\n'), - Buffer.from(`${info.xref.startingIndex} 1\n`), - Buffer.from(rows.join('\n')), - Buffer.from('\ntrailer\n'), - Buffer.from('<<\n'), - Buffer.from(`/Size ${info.xref.maxIndex + 1}\n`), - Buffer.from(`/Root ${info.rootRef}\n`), - Buffer.from(info.infoRef ? `/Info ${info.infoRef}\n` : ''), - Buffer.from(`/Prev ${info.xRefPosition}\n`), - Buffer.from('>>\n'), - Buffer.from('startxref\n'), - Buffer.from(`${pdf.length}\n`), - Buffer.from('%%EOF'), - ]); -}; - -export default createBufferTrailer; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferTrailer.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferTrailer.min.js new file mode 100644 index 000000000..521bef324 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/createBufferTrailer.min.js @@ -0,0 +1,8 @@ +const createBufferTrailer=(f,r,e)=>{let o=[];return o[0]="0000000000 65535 f ",e.forEach((f,r)=>{f=("0000000000"+f).slice(-10);o[r+1]=r+` 1 +${f} 00000 n `}),o=o.filter(f=>void 0!==f),Buffer.concat([Buffer.from("xref\n"),Buffer.from(r.xref.startingIndex+` 1 +`),Buffer.from(o.join("\n")),Buffer.from("\ntrailer\n"),Buffer.from("<<\n"),Buffer.from(`/Size ${r.xref.maxIndex+1} +`),Buffer.from(`/Root ${r.rootRef} +`),Buffer.from(r.infoRef?`/Info ${r.infoRef} +`:""),Buffer.from(`/Prev ${r.xRefPosition} +`),Buffer.from(">>\n"),Buffer.from("startxref\n"),Buffer.from(f.length+` +`),Buffer.from("%%EOF")])};export default createBufferTrailer; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/findObject.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/findObject.js deleted file mode 100644 index 85679f1dd..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/findObject.js +++ /dev/null @@ -1,21 +0,0 @@ -import getIndexFromRef from './getIndexFromRef.js'; - -/** - * @param {Buffer} pdf - * @param {Map} refTable - * @returns {object} - */ -const findObject = (pdf, refTable, ref) => { - const index = getIndexFromRef(refTable, ref); - - const offset = refTable.offsets.get(index); - let slice = pdf.slice(offset); - slice = slice.slice(0, slice.indexOf('endobj', 'utf8')); - - // FIXME: What if it is a stream? - slice = slice.slice(slice.indexOf('<<', 'utf8') + 2); - slice = slice.slice(0, slice.lastIndexOf('>>', 'utf8')); - return slice; -}; - -export default findObject; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/findObject.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/findObject.min.js new file mode 100644 index 000000000..dd73ad2d9 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/findObject.min.js @@ -0,0 +1 @@ +import getIndexFromRef from"./getIndexFromRef.min.js";const findObject=(e,t,f)=>{f=getIndexFromRef(t,f),t=t.offsets.get(f);let n=e.slice(t);return n=(n=(n=n.slice(0,n.indexOf("endobj","utf8"))).slice(n.indexOf("<<","utf8")+2)).slice(0,n.lastIndexOf(">>","utf8"))};export default findObject; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getIndexFromRef.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getIndexFromRef.js deleted file mode 100644 index bbfc6cd86..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getIndexFromRef.js +++ /dev/null @@ -1,21 +0,0 @@ -import SignPdfError from './SignPdfError.js'; - -/** - * @param {object} refTable - * @param {string} ref - * @returns {number} - */ -const getIndexFromRef = (refTable, ref) => { - let [index] = ref.split(' '); - index = parseInt(index); - - if (!refTable.offsets.has(index)) { - throw new SignPdfError( - `Failed to locate object "${ref}".`, - SignPdfError.TYPE_PARSE, - ); - } - return index; -}; - -export default getIndexFromRef; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getIndexFromRef.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getIndexFromRef.min.js new file mode 100644 index 000000000..cf34f0b0a --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getIndexFromRef.min.js @@ -0,0 +1 @@ +import SignPdfError from"./SignPdfError.min.js";const getIndexFromRef=(r,e)=>{var[o]=e.split(" "),o=parseInt(o);if(r.offsets.has(o))return o;throw new SignPdfError(`Failed to locate object "${e}".`,SignPdfError.TYPE_PARSE)};export default getIndexFromRef; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPageRef.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPageRef.js deleted file mode 100644 index a3b1f9313..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPageRef.js +++ /dev/null @@ -1,41 +0,0 @@ -import getPagesDictionaryRef from "./getPagesDictionaryRef.js"; -import findObject from "./findObject.js"; - -/** - * Finds the reference to a page. - * - * @param {Buffer} pdfBuffer - * @param {Object} info As extracted from readRef() - * @param {Number} Page is pageNo on which we want widget/sign - */ -export default function getPageRef(pdfBuffer, info, Page) { - 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(); - // console.log("pages ", pages); - // const split = pages.trim().split(" ", 3); - // return `${split[0]} ${split[1]} ${split[2]}`; - - // below code is used to get pageIndex (10 0 R) from pageNo to add sign widget on it - let indices = []; - for (var i = 0; i < pages.length; i++) { - if (pages[i] === "R") indices.push(i); - } - let pageIndex = {}; - let startPosition = 0; - indices.forEach((x, index) => { - pageIndex = { - ...pageIndex, - [index + 1]: pages.substring(startPosition + 1, x + 1), - }; - startPosition = x + 1; - }); - // console.log("pageIndex ", pageIndex); - - const pageStr = Page ? pageIndex[Page] : pageIndex[1]; - // console.log("pageStr ", pageStr); - return pageStr; -} diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPageRef.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPageRef.min.js new file mode 100644 index 000000000..88dd52a0f --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPageRef.min.js @@ -0,0 +1 @@ +import getPagesDictionaryRef from"./getPagesDictionaryRef.min.js";import findObject from"./findObject.min.js";export default function getPageRef(e,t,i){var n=getPagesDictionaryRef(t),e=findObject(e,t.xref,n),t=e.indexOf("/Kids"),n=e.indexOf("[",t)+1,t=e.indexOf("]",t);const f=e.slice(n,t).toString();for(var r=[],o=0;o{a={...a,[t+1]:f.substring(g+1,e+1)},g=e+1}),i?a[i]:a[1]} \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPagesDictionaryRef.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPagesDictionaryRef.js deleted file mode 100644 index ae375bbf2..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPagesDictionaryRef.js +++ /dev/null @@ -1,18 +0,0 @@ -import SignPdfError from './SignPdfError.js'; - -/** - * @param {Object} info As extracted from readRef() - */ - -export default function getPagesDictionaryRef(info) { - const pagesRefRegex = /\/Pages\s+(\d+\s+\d+\s+R)/g; - const match = pagesRefRegex.exec(info.root); - if (match === null) { - throw new SignPdfError( - 'Failed to find the pages descriptor. This is probably a problem in node-signpdf.', - SignPdfError.TYPE_PARSE, - ); - } - - return match[1]; -} diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPagesDictionaryRef.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPagesDictionaryRef.min.js new file mode 100644 index 000000000..98d50586e --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/getPagesDictionaryRef.min.js @@ -0,0 +1 @@ +import SignPdfError from"./SignPdfError.min.js";export default function getPagesDictionaryRef(r){r=/\/Pages\s+(\d+\s+\d+\s+R)/g.exec(r.root);if(null===r)throw new SignPdfError("Failed to find the pages descriptor. This is probably a problem in node-signpdf.",SignPdfError.TYPE_PARSE);return r[1]} \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfObject.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfObject.js deleted file mode 100644 index fdcd9c611..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfObject.js +++ /dev/null @@ -1,131 +0,0 @@ -/* -PDFObject by Devon Govett used below. -The class is part of pdfkit. See https://github.com/foliojs/pdfkit -LICENSE: MIT. Included in this folder. -Modifications may have been applied for the purposes of node-signpdf. -*/ - -import PDFAbstractReference from './PDFAbstractReference.js'; -/* -PDFObject - converts JavaScript types into their corresponding PDF types. -By Devon Govett -*/ - -const pad = (str, length) => (Array(length + 1).join('0') + str).slice(-length); - -const escapableRe = /[\n\r\t\b\f()\\]/g; -const escapable = { - '\n': '\\n', - '\r': '\\r', - '\t': '\\t', - '\b': '\\b', - '\f': '\\f', - '\\': '\\\\', - '(': '\\(', - ')': '\\)', -}; - -// Convert little endian UTF-16 to big endian -const swapBytes = (buff) => buff.swap16(); - -export default class PDFObject { - static convert(object, encryptFn = null) { - // String literals are converted to the PDF name type - if (typeof object === 'string') { - return `/${object}`; - - // String objects are converted to PDF strings (UTF-16) - } if (object instanceof String) { - let string = object; - // Detect if this is a unicode string - let isUnicode = false; - for (let i = 0, end = string.length; i < end; i += 1) { - if (string.charCodeAt(i) > 0x7f) { - isUnicode = true; - break; - } - } - - // If so, encode it as big endian UTF-16 - let stringBuffer; - if (isUnicode) { - stringBuffer = swapBytes(Buffer.from(`\ufeff${string}`, 'utf16le')); - } else { - stringBuffer = Buffer.from(string, 'ascii'); - } - - // Encrypt the string when necessary - if (encryptFn) { - string = encryptFn(stringBuffer).toString('binary'); - } else { - string = stringBuffer.toString('binary'); - } - - // Escape characters as required by the spec - string = string.replace(escapableRe, (c) => escapable[c]); - - return `(${string})`; - - // Buffers are converted to PDF hex strings - } if (Buffer.isBuffer(object)) { - return `<${object.toString('hex')}>`; - } if (object instanceof PDFAbstractReference) { - return object.toString(); - } if (object instanceof Date) { - let string = `D:${pad(object.getUTCFullYear(), 4)}${pad(object.getUTCMonth() + 1, 2)}${pad(object.getUTCDate(), 2)}${pad(object.getUTCHours(), 2)}${pad(object.getUTCMinutes(), 2)}${pad(object.getUTCSeconds(), 2)}Z`; - - // Encrypt the string when necessary - if (encryptFn) { - string = encryptFn(Buffer.from(string, 'ascii')).toString('binary'); - - // Escape characters as required by the spec - string = string.replace(escapableRe, (c) => escapable[c]); - } - - return `(${string})`; - } if (Array.isArray(object)) { - const items = object.map((e) => PDFObject.convert(e, encryptFn)).join(' '); - return `[${items}]`; - } - if ({}.toString.call(object) === '[object Object]') { - const out = ['<<']; - let streamData; - - // @todo this can probably be refactored into a reduce - Object.entries(object).forEach(([key, val]) => { - let checkedValue = ''; - - if (val && val.toString().indexOf('<<') !== -1) { - checkedValue = val; - } else { - checkedValue = PDFObject.convert(val, encryptFn); - } - - // // if (key === 'stream') { - // // streamData = `${key}\n${val}\nendstream`; - // // } - // else { - out.push(`/${key} ${checkedValue}`); - // } - }); - out.push('>>'); - - // if (streamData) { - // out.push(streamData); - // } - return out.join('\n'); - } - if (typeof object === 'number') { - return PDFObject.number(object); - } - return `${object}`; - } - - static number(n) { - if (n > -1e21 && n < 1e21) { - return Math.round(n * 1e6) / 1e6; - } - - throw new Error(`unsupported number: ${n}`); - } -} diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfObject.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfObject.min.js new file mode 100644 index 000000000..9105ea27a --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfObject.min.js @@ -0,0 +1 @@ +import PDFAbstractReference from"./PDFAbstractReference.min.js";const pad=(e,t)=>(Array(t+1).join("0")+e).slice(-t),escapableRe=/[\n\r\t\b\f()\\]/g,escapable={"\n":"\\n","\r":"\\r","\t":"\\t","\b":"\\b","\f":"\\f","\\":"\\\\","(":"\\(",")":"\\)"},swapBytes=e=>e.swap16();export default class PDFObject{static convert(t,a=null){if("string"==typeof t)return"/"+t;if(t instanceof String){let r=t,n=!1;for(let e=0,t=r.length;eescapable[e])})`}if(Buffer.isBuffer(t))return`<${t.toString("hex")}>`;if(t instanceof PDFAbstractReference)return t.toString();if(t instanceof Date){let e=`D:${pad(t.getUTCFullYear(),4)}${pad(t.getUTCMonth()+1,2)}${pad(t.getUTCDate(),2)}${pad(t.getUTCHours(),2)}${pad(t.getUTCMinutes(),2)}${pad(t.getUTCSeconds(),2)}Z`;return`(${e=a?(e=a(Buffer.from(e,"ascii")).toString("binary")).replace(escapableRe,e=>escapable[e]):e})`}if(Array.isArray(t))return`[${t.map(e=>PDFObject.convert(e,a)).join(" ")}]`;if("[object Object]"!=={}.toString.call(t))return"number"==typeof t?PDFObject.number(t):""+t;{const n=["<<"];return Object.entries(t).forEach(([e,t])=>{let r="";r=t&&-1!==t.toString().indexOf("<<")?t:PDFObject.convert(t,a),n.push(`/${e} `+r)}),n.push(">>"),n.join("\n")}}static number(e){if(-1e21>\n/Font <<\n/f1 ${APFONT.index} 0 R\n>>\n>>`, - MediaBox: [0, 0, 197, 70], - Subtype: "Form", - }, - null, - getStream(IMG.index) - ); -} - -const getStream = (imgIndex) => { - // (Aláírta: ${userInformation.commonName}) Tj - // (Aláírta: testing name) Tj - // (${new Date().toISOString().slice(0, 10)}) Tj - - return getConvertedText(` - 1.0 1.0 1.0 rg - 0.0 0.0 0.0 RG - q - q - 200 0 0 50 0 10 cm - /Img${imgIndex} Do - Q - 0 0 0 rg - BT - 0 Tr - /f1 10.0 Tf - 1.4 0 0 1 20 45.97412 Tm - ET - BT - 0 Tr - /f1 10.0 Tf - 1.4 0 0 1 20 33.56006 Tm - ET - Q`); -}; - -const getFont = (pdf, baseFont) => { - return pdf.ref({ - Type: "Font", - BaseFont: baseFont, - Encoding: "WinAnsiEncoding", - Subtype: "Type1", - }); -}; - -const getJpgImage = (pdf, data) => { - if (data.readUInt16BE(0) !== 0xffd8) { - throw "SOI not found in JPEG"; - } - - let pos = 2; - let marker; - while (pos < data.length) { - marker = data.readUInt16BE(pos); - pos += 2; - if (MARKERS.includes(marker)) { - break; - } - pos += data.readUInt16BE(pos); - } - - if (!MARKERS.includes(marker)) { - throw "Invalid JPEG."; - } - pos += 2; - - const bits = data[pos++]; - const height = data.readUInt16BE(pos); - pos += 2; - - const width = data.readUInt16BE(pos); - pos += 2; - - const channels = data[pos++]; - const colorSpace = COLOR_SPACE_MAP[channels]; - - const baseJpgData = { - Type: "XObject", - Subtype: "Image", - BitsPerComponent: bits, - Width: width, - Height: height, - ColorSpace: colorSpace, - Filter: "DCTDecode", - }; - - if (colorSpace === "DeviceCMYK") { - baseJpgData["Decode"] = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]; - } - const image = pdf.ref(baseJpgData, null, data); - - return image; -}; - -const getPngImage = (pdf, data) => { - const image = new PNG(data); - const hasAlphaChannel = image.hasAlphaChannel; - - const pngBaseData = { - Type: "XObject", - Subtype: "Image", - BitsPerComponent: hasAlphaChannel ? 8 : image.bits, - Width: image.width, - Height: image.height, - Filter: "FlateDecode", - }; - - if (!hasAlphaChannel) { - const params = pdf.ref({ - Predictor: 15, - Colors: image.colors, - BitsPerComponent: image.bits, - Columns: image.width, - }); - - pngBaseData["DecodeParms"] = params; - } - - if (image.palette.length === 0) { - pngBaseData["ColorSpace"] = image.colorSpace; - } else { - const palette = pdf.ref({ - stream: new Buffer(image.palette), - }); - pngBaseData["ColorSpace"] = [ - "Indexed", - "DeviceRGB", - image.palette.length / 3 - 1, - palette, - ]; - } - - if (image.transparency.grayscale != null) { - const val = image.transparency.grayscale; - pngBaseData["Mask"] = [val, val]; - } else if (image.transparency.rgb) { - const { rgb } = image.transparency; - const mask = []; - - for (let x of rgb) { - mask.push(x, x); - } - pngBaseData["Mask"] = mask; - } else if (image.transparency.indexed) { - loadIndexedAlphaChannel(image); - } else if (hasAlphaChannel) { - splitAlphaChannel(image); - const sMask = getSmask(pdf, image); - pngBaseData["Mask"] = sMask; - } - - const pngImage = pdf.ref(pngBaseData, null, image.imgData); - - return pngImage; -}; - -const loadIndexedAlphaChannel = (image) => { - const transparency = image.transparency.indexed; - return image.decodePixels((pixels) => { - const alphaChannel = new Buffer(image.width * image.height); - - let i = 0; - for (let j = 0, end = pixels.length; j < end; j++) { - alphaChannel[i++] = transparency[pixels[j]]; - } - - image.alphaChannel = zlib.deflateSync(alphaChannel); - }); -}; - -const splitAlphaChannel = (image) => { - image.decodePixels((pixels) => { - let a, p; - const colorCount = image.colors; - const pixelCount = image.width * image.height; - const imgData = new Buffer(pixelCount * colorCount); - const alphaChannel = new Buffer(pixelCount); - - let i = (p = a = 0); - const len = pixels.length; - const skipByteCount = image.bits === 16 ? 1 : 0; - while (i < len) { - for (let colorIndex = 0; colorIndex < colorCount; colorIndex++) { - imgData[p++] = pixels[i++]; - i += skipByteCount; - } - alphaChannel[a++] = pixels[i++]; - i += skipByteCount; - } - - image.imgData = zlib.deflateSync(imgData); - image.alphaChannel = zlib.deflateSync(alphaChannel); - }); -}; - -const getSmask = (pdf, image) => { - let sMask; - console.log("image.hasAlphaChannel ", image.hasAlphaChannel); - if (image.hasAlphaChannel) { - console.log("image.alphaChannel ", image.alphaChannel); - - sMask = pdf.ref({ - Type: "XObject", - Subtype: "Image", - Height: image.height, - Width: image.width, - BitsPerComponent: 8, - Filter: "FlateDecode", - ColorSpace: "DeviceGray", - Decode: [0, 1], - stream: image.alphaChannel, - }); - } - - return sMask; -}; - -const getConvertedText = (text) => { - return text - .split("") - .map((character) => { - return specialCharacters.includes(character) - ? getOctalCodeFromCharacter(character) - : character; - }) - .join(""); -}; - -const getOctalCodeFromCharacter = (character) => { - return "\\" + character.charCodeAt(0).toString(8); -}; - -// -const pdfkitAddPlaceholder = ({ - pdf, - pdfBuffer, - reason, - contactInfo = "emailfromp1289@gmail.com", - name = "Name from p12", - location = "Location from p12", - signatureLength = DEFAULT_SIGNATURE_LENGTH, - byteRangePlaceholder = DEFAULT_BYTE_RANGE_PLACEHOLDER, - subFilter = SUBFILTER_ADOBE_PKCS7_DETACHED, - sign, -}) => { - // testing - const FONT = getFont(pdf, "Helvetica"); - const ZAF = getFont(pdf, "ZapfDingbats"); - const APFONT = getFont(pdf, "Helvetica"); - // const imagePath = "exports/img.jpg"; - const signBase64 = sign.Base64; - const imagePath = Buffer.from(signBase64, "base64"); - - const IMG = getImage(imagePath, pdf); - const AP = getAnnotationApparance(pdf, IMG, APFONT); - - // - - /* eslint-disable no-underscore-dangle,no-param-reassign */ - // Generate the signature placeholder - const signature = pdf.ref({ - Type: "Sig", - Filter: "Adobe.PPKLite", - SubFilter: subFilter, - ByteRange: [ - 0, - byteRangePlaceholder, - byteRangePlaceholder, - byteRangePlaceholder, - ], - Contents: Buffer.from(String.fromCharCode(0).repeat(signatureLength)), - Reason: new String(reason), // eslint-disable-line no-new-wrappers - M: new Date(), - ContactInfo: new String(contactInfo), // eslint-disable-line no-new-wrappers - Name: new String(name), // eslint-disable-line no-new-wrappers - Location: new String(location), // eslint-disable-line no-new-wrappers - }); - - // Check if pdf already contains acroform field - const acroFormPosition = pdfBuffer.lastIndexOf("/Type /AcroForm"); - const isAcroFormExists = acroFormPosition !== -1; - let fieldIds = []; - let acroFormId; - - if (isAcroFormExists) { - let acroFormStart = acroFormPosition; - // 10 is the distance between "/Type /AcroForm" and AcroFrom ID - const charsUntilIdEnd = 10; - const acroFormIdEnd = acroFormPosition - charsUntilIdEnd; - // Let's find AcroForm ID by trying to find the "\n" before the ID - // 12 is a enough space to find the "\n" - // (generally it's 2 or 3, but I'm giving a big space though) - const maxAcroFormIdLength = 12; - let foundAcroFormId = ""; - let index = charsUntilIdEnd + 1; - for (index; index < charsUntilIdEnd + maxAcroFormIdLength; index += 1) { - const acroFormIdString = pdfBuffer - .slice(acroFormPosition - index, acroFormIdEnd) - .toString(); - - if (acroFormIdString[0] === "\n") { - break; - } - - foundAcroFormId = acroFormIdString; - acroFormStart = acroFormPosition - index; - } - - const pdfSlice = pdfBuffer.slice(acroFormStart); - const acroForm = pdfSlice.slice(0, pdfSlice.indexOf("endobj")).toString(); - acroFormId = parseInt(foundAcroFormId); - - const acroFormFields = acroForm.slice( - acroForm.indexOf("/Fields [") + 9, - acroForm.indexOf("]") - ); - fieldIds = acroFormFields - .split(" ") - .filter((element, i) => i % 3 === 0) - .map((fieldId) => new PDFKitReferenceMock(fieldId)); - } - const signatureName = "Signature"; - const signatureLeftOffset = fieldIds.length * 125; - const signatureBottomOffset = 5; - const left = sign.Left; //461.50001525878906 //477.3333282470703//409.5000305175781; // first block 44.33332824707031; - const bottom = sign.Bottom; // 730.1979598999023 - 39 + 5.072906494140625 //720.1979598999023 - 30 + 15.072906494140625; // first block 652.1979598999023 - 60 + 19.072906494140625; - const RightX = left + sign.Width; // 98; - const RightY = bottom + sign.Height; //39; - // Generate signature annotation widget - const widget = pdf.ref({ - Type: "Annot", - Subtype: "Widget", - FT: "Sig", - Rect: [left, bottom, RightX, RightY], // [signatureLeftOffset, 0, signatureLeftOffset + 90, signatureBottomOffset + 60, ], // [50, 50, 100, 100], //[25, 25, 100, 300], // - V: signature, - T: new String(signatureName + (fieldIds.length + 1)), // eslint-disable-line no-new-wrappers - F: 4, - P: pdf.page.dictionary, // eslint-disable-line no-underscore-dangle - AP: `<>`, // testing - DA: new String("/Helvetica 0 Tf 0 g"), // eslint-disable-line no-new-wrappers // testing - }); - pdf.page.dictionary.data.Annots = [widget]; - // Include the widget in a page - let form; - - if (!isAcroFormExists) { - // Create a form (with the widget) and link in the _root - form = pdf.ref({ - Type: "AcroForm", - SigFlags: 3, - Fields: [...fieldIds, widget], - }); - } else { - // Use existing acroform and extend the fields with newly created widgets - form = pdf.ref( - { - Type: "AcroForm", - SigFlags: 3, - Fields: [...fieldIds, widget], - DR: `<>\n>>`, /// testing - }, - acroFormId - ); - } - pdf._root.data.AcroForm = form; - - return { - signature, - form, - widget, - }; - /* eslint-enable no-underscore-dangle,no-param-reassign */ -}; - -export default pdfkitAddPlaceholder; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfkitAddPlaceholder.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfkitAddPlaceholder.min.js new file mode 100644 index 000000000..21a62665e --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/pdfkitAddPlaceholder.min.js @@ -0,0 +1,28 @@ +import{DEFAULT_BYTE_RANGE_PLACEHOLDER,DEFAULT_SIGNATURE_LENGTH,SUBFILTER_ADOBE_PKCS7_DETACHED}from"./const.min.js";import PDFKitReferenceMock from"./PDFKitReferenceMock.min.js";import PNG from"png-js";import zlib from"zlib";import fs from"node:fs";const specialCharacters=["á","Á","é","É","í","Í","ó","Ó","ö","Ö","ő","Ő","ú","Ú","ű","Ű"],MARKERS=[65472,65473,65474,65475,65477,65478,65479,65480,65481,65482,65483,65484,65485,65486,65487],COLOR_SPACE_MAP={1:"DeviceGray",3:"DeviceRGB",4:"DeviceCMYK"};function getImage(e,t){let n;if(255===e[0]&&216===e[1])n=getJpgImage(t,e);else{if(137!==e[0]||"PNG"!==e.toString("ascii",1,4))throw new Error("Unknown image format.");n=getPngImage(t,e)}return n}function getAnnotationApparance(e,t,n){return e.ref({CropBox:[0,0,197,70],Type:"XObject",FormType:1,BBox:[0,0,197,70],Resources:`<> +/Font << +/f1 ${n.index} 0 R +>> +>>`,MediaBox:[0,0,197,70],Subtype:"Form"},null,getStream(t.index))}const getStream=e=>getConvertedText(` + 1.0 1.0 1.0 rg + 0.0 0.0 0.0 RG + q + q + 200 0 0 50 0 10 cm + /Img${e} Do + Q + 0 0 0 rg + BT + 0 Tr + /f1 10.0 Tf + 1.4 0 0 1 20 45.97412 Tm + ET + BT + 0 Tr + /f1 10.0 Tf + 1.4 0 0 1 20 33.56006 Tm + ET + Q`),getFont=(e,t)=>e.ref({Type:"Font",BaseFont:t,Encoding:"WinAnsiEncoding",Subtype:"Type1"}),getJpgImage=(e,t)=>{if(65496!==t.readUInt16BE(0))throw"SOI not found in JPEG";let n=2,a;for(;n{var t=new PNG(t),n=t.hasAlphaChannel,a={Type:"XObject",Subtype:"Image",BitsPerComponent:n?8:t.bits,Width:t.width,Height:t.height,Filter:"FlateDecode"};if(n||(r=e.ref({Predictor:15,Colors:t.colors,BitsPerComponent:t.bits,Columns:t.width}),a.DecodeParms=r),0===t.palette.length?a.ColorSpace=t.colorSpace:(r=e.ref({stream:new Buffer(t.palette)}),a.ColorSpace=["Indexed","DeviceRGB",t.palette.length/3-1,r]),null!=t.transparency.grayscale){var r=t.transparency.grayscale;a.Mask=[r,r]}else if(t.transparency.rgb){var o,r=t.transparency["rgb"],i=[];for(o of r)i.push(o,o);a.Mask=i}else t.transparency.indexed?loadIndexedAlphaChannel(t):n&&(splitAlphaChannel(t),r=getSmask(e,t),a.Mask=r);return e.ref(a,null,t.imgData)},loadIndexedAlphaChannel=e=>{const o=e.transparency.indexed;return e.decodePixels(n=>{var a=new Buffer(e.width*e.height);let r=0;for(let e=0,t=n.length;e{d.decodePixels(t=>{let e,n;var a=d.colors,r=d.width*d.height,o=new Buffer(r*a),i=new Buffer(r);let l=n=e=0;for(var g=t.length,c=16===d.bits?1:0;l{let n;return console.log("image.hasAlphaChannel ",t.hasAlphaChannel),t.hasAlphaChannel&&(console.log("image.alphaChannel ",t.alphaChannel),n=e.ref({Type:"XObject",Subtype:"Image",Height:t.height,Width:t.width,BitsPerComponent:8,Filter:"FlateDecode",ColorSpace:"DeviceGray",Decode:[0,1],stream:t.alphaChannel})),n},getConvertedText=e=>e.split("").map(e=>specialCharacters.includes(e)?getOctalCodeFromCharacter(e):e).join(""),getOctalCodeFromCharacter=e=>"\\"+e.charCodeAt(0).toString(8),pdfkitAddPlaceholder=({pdf:e,pdfBuffer:a,reason:r,contactInfo:t="emailfromp1289@gmail.com",name:n="Name from p12",location:o="Location from p12",signatureLength:i=DEFAULT_SIGNATURE_LENGTH,byteRangePlaceholder:l=DEFAULT_BYTE_RANGE_PLACEHOLDER,subFilter:g=SUBFILTER_ADOBE_PKCS7_DETACHED,sign:c})=>{var d=getFont(e,"Helvetica"),s=getFont(e,"ZapfDingbats"),f=getFont(e,"Helvetica"),p=c.Base64,p=getAnnotationApparance(e,getImage(Buffer.from(p,"base64"),e),f),f=e.ref({Type:"Sig",Filter:"Adobe.PPKLite",SubFilter:g,ByteRange:[0,l,l,l],Contents:Buffer.from(String.fromCharCode(0).repeat(i)),Reason:new String(r),M:new Date,ContactInfo:new String(t),Name:new String(n),Location:new String(o)}),h=a.lastIndexOf("/Type /AcroForm"),g=-1!==h;let m=[],C;if(g){let e=h;var S=h-10;let t="",n=11;for(n;n<22;n+=1){var A=a.slice(h-n,S).toString();if("\n"===A[0])break;t=A,e=h-n}l=a.slice(e),i=l.slice(0,l.indexOf("endobj")).toString(),r=(C=parseInt(t),i.slice(i.indexOf("/Fields [")+9,i.indexOf("]")));m=r.split(" ").filter((e,t)=>t%3==0).map(e=>new PDFKitReferenceMock(e))}m.length;t=c.Left,n=c.Bottom,o=t+c.Width,l=n+c.Height,i=e.ref({Type:"Annot",Subtype:"Widget",FT:"Sig",Rect:[t,n,o,l],V:f,T:new String("Signature"+(m.length+1)),F:4,P:e.page.dictionary,AP:`<>`,DA:new String("/Helvetica 0 Tf 0 g")});e.page.dictionary.data.Annots=[i];let T;return T=g?e.ref({Type:"AcroForm",SigFlags:3,Fields:[...m,i],DR:`<> +>>`},C):e.ref({Type:"AcroForm",SigFlags:3,Fields:[...m,i]}),{signature:f,form:e._root.data.AcroForm=T,widget:i}};export default pdfkitAddPlaceholder; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/plainplaceholder.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/plainplaceholder.js deleted file mode 100644 index 32c64046d..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/plainplaceholder.js +++ /dev/null @@ -1,138 +0,0 @@ -import PDFObject from "./pdfObject.js"; -import PDFKitReferenceMock from "./PDFKitReferenceMock.js"; -import removeTrailingNewLine from "./removeTrailingNewLine.js"; -import { - DEFAULT_SIGNATURE_LENGTH, - SUBFILTER_ADOBE_PKCS7_DETACHED, -} from "./const.js"; -import pdfkitAddPlaceholder from "./pdfkitAddPlaceholder.js"; - -import getIndexFromRef from "./getIndexFromRef.js"; -import readPdf from "./readPdf.js"; -import getPageRef from "./getPageRef.js"; -import createBufferRootWithAcroform from "./createBufferRootWithAcroform.js"; -import createBufferPageWithAnnotation from "./createBufferPageWithAnnotation.js"; -import createBufferTrailer from "./createBufferTrailer.js"; - -const isContainBufferRootWithAcroform = (pdf) => { - const bufferRootWithAcroformRefRegex = /\/AcroForm\s+(\d+\s\d+\sR)/g; - const match = bufferRootWithAcroformRefRegex.exec(pdf.toString()); - - return match != null && match[1] != null && match[1] !== ""; -}; - -// testing - -const getAssembledPdf = (pdf, index, input, stream) => { - let finalPdf = pdf; - - finalPdf = Buffer.concat([ - finalPdf, - Buffer.from("\n"), - Buffer.from(`${index} 0 obj\n`), - Buffer.from(PDFObject.convert(input)), - ]); - - if (stream) { - finalPdf = Buffer.concat([ - finalPdf, - Buffer.from("\nstream\n"), - Buffer.from(stream), - Buffer.from("\nendstream"), - ]); - } - - finalPdf = Buffer.concat([finalPdf, Buffer.from("\nendobj\n")]); - - return finalPdf; -}; - -// -/** - * Adds a signature placeholder to a PDF Buffer. - * - * This contrasts with the default pdfkit-based implementation. - * Parsing is done using simple string operations. - * Adding is done with `Buffer.concat`. - * This allows node-signpdf to be used on any PDF and - * not only on a freshly created through PDFKit one. - */ -const plainAddPlaceholder = ({ - pdfBuffer, - reason, - contactInfo = "emailfromp1289@gmail.com", - name = "Name from p12", - location = "Location from p12", - signatureLength = DEFAULT_SIGNATURE_LENGTH, - subFilter = SUBFILTER_ADOBE_PKCS7_DETACHED, - sign, -}) => { - let pdf = removeTrailingNewLine(pdfBuffer); - const info = readPdf(pdf); - const pageRef = getPageRef(pdf, info, sign.Page); - const pageIndex = getIndexFromRef(info.xref, pageRef); - const addedReferences = new Map(); - - const pdfKitMock = { - ref: (input, additionalIndex, stream) => { - info.xref.maxIndex += 1; - - const index = - additionalIndex != null ? additionalIndex : info.xref.maxIndex; - - addedReferences.set(index, pdf.length + 1); // + 1 new line - - pdf = getAssembledPdf(pdf, index, input, stream); - - return new PDFKitReferenceMock(info.xref.maxIndex); - }, - page: { - dictionary: new PDFKitReferenceMock(pageIndex, { - data: { - Annots: [], - }, - }), - }, - _root: { - data: {}, - }, - }; - - const { form, widget } = pdfkitAddPlaceholder({ - pdf: pdfKitMock, - pdfBuffer, - reason, - contactInfo, - name, - location, - signatureLength, - subFilter, - sign, - }); - - if (!isContainBufferRootWithAcroform(pdf)) { - const rootIndex = getIndexFromRef(info.xref, info.rootRef); - addedReferences.set(rootIndex, pdf.length + 1); - pdf = Buffer.concat([ - pdf, - Buffer.from("\n"), - createBufferRootWithAcroform(pdf, info, form), - ]); - } - addedReferences.set(pageIndex, pdf.length + 1); - pdf = Buffer.concat([ - pdf, - Buffer.from("\n"), - createBufferPageWithAnnotation(pdf, info, pageRef, widget), - ]); - - pdf = Buffer.concat([ - pdf, - Buffer.from("\n"), - createBufferTrailer(pdf, info, addedReferences), - ]); - - return pdf; -}; - -export default plainAddPlaceholder; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/plainplaceholder.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/plainplaceholder.min.js new file mode 100644 index 000000000..648fabb49 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/plainplaceholder.min.js @@ -0,0 +1,2 @@ +import PDFObject from"./pdfObject.min.js";import PDFKitReferenceMock from"./PDFKitReferenceMock.min.js";import removeTrailingNewLine from"./removeTrailingNewLine.min.js";import{DEFAULT_SIGNATURE_LENGTH,SUBFILTER_ADOBE_PKCS7_DETACHED}from"./const.min.js";import pdfkitAddPlaceholder from"./pdfkitAddPlaceholder.min.js";import getIndexFromRef from"./getIndexFromRef.min.js";import readPdf from"./readPdf.min.js";import getPageRef from"./getPageRef.min.js";import createBufferRootWithAcroform from"./createBufferRootWithAcroform.min.js";import createBufferPageWithAnnotation from"./createBufferPageWithAnnotation.min.js";import createBufferTrailer from"./createBufferTrailer.min.js";const isContainBufferRootWithAcroform=e=>{e=/\/AcroForm\s+(\d+\s\d+\sR)/g.exec(e.toString());return null!=e&&null!=e[1]&&""!==e[1]},getAssembledPdf=(e,r,f,o)=>{let t=e;return t=Buffer.concat([t,Buffer.from("\n"),Buffer.from(r+` 0 obj +`),Buffer.from(PDFObject.convert(f))]),o&&(t=Buffer.concat([t,Buffer.from("\nstream\n"),Buffer.from(o),Buffer.from("\nendstream")])),t=Buffer.concat([t,Buffer.from("\nendobj\n")])},plainAddPlaceholder=({pdfBuffer:e,reason:r,contactInfo:f="emailfromp1289@gmail.com",name:o="Name from p12",location:t="Location from p12",signatureLength:n=DEFAULT_SIGNATURE_LENGTH,subFilter:i=SUBFILTER_ADOBE_PKCS7_DETACHED,sign:m})=>{let a=removeTrailingNewLine(e);const c=readPdf(a);var d=getPageRef(a,c,m.Page),u=getIndexFromRef(c.xref,d);const l=new Map;var s={ref:(e,r,f)=>{c.xref.maxIndex+=1;r=null!=r?r:c.xref.maxIndex;return l.set(r,a.length+1),a=getAssembledPdf(a,r,e,f),new PDFKitReferenceMock(c.xref.maxIndex)},page:{dictionary:new PDFKitReferenceMock(u,{data:{Annots:[]}})},_root:{data:{}}},{form:s,widget:e}=pdfkitAddPlaceholder({pdf:s,pdfBuffer:e,reason:r,contactInfo:f,name:o,location:t,signatureLength:n,subFilter:i,sign:m});return isContainBufferRootWithAcroform(a)||(r=getIndexFromRef(c.xref,c.rootRef),l.set(r,a.length+1),a=Buffer.concat([a,Buffer.from("\n"),createBufferRootWithAcroform(a,c,s)])),l.set(u,a.length+1),a=Buffer.concat([a,Buffer.from("\n"),createBufferPageWithAnnotation(a,c,d,e)]),a=Buffer.concat([a,Buffer.from("\n"),createBufferTrailer(a,c,l)])};export default plainAddPlaceholder; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readPdf.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readPdf.js deleted file mode 100644 index d9e85eecb..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readPdf.js +++ /dev/null @@ -1,54 +0,0 @@ -import readRefTable from './readRefTable.js'; -import findObject from './findObject.js'; - -const getValue = (trailer, key) => { - let index = trailer.indexOf(key); - - if (index === -1) { - return undefined; - } - - const slice = trailer.slice(index); - index = slice.indexOf('/', 1); - if (index === -1) { - index = slice.indexOf('>', 1); - } - return slice.slice(key.length + 1, index).toString().trim(); // key + at least one space -}; - -/** - * Simplified parsing of a PDF Buffer. - * Extracts reference table, root info and trailer start. - * - * See section 7.5.5 (File Trailer) of the PDF specs. - * - * @param {Buffer} pdfBuffer - */ -const readPdf = (pdfBuffer) => { - // Extract the trailer dictionary. - const trailerStart = pdfBuffer.lastIndexOf('trailer'); - // The trailer is followed by xref. Then an EOF. EOF's length is 6 characters. - const trailer = pdfBuffer.slice(trailerStart, pdfBuffer.length - 6); - - let xRefPosition = trailer.slice(trailer.lastIndexOf('startxref') + 10).toString(); - - xRefPosition = parseInt(xRefPosition); - const refTable = readRefTable(pdfBuffer); - - const rootRef = getValue(trailer, '/Root'); - const root = findObject(pdfBuffer, refTable, rootRef).toString(); - - const infoRef = getValue(trailer, '/Info'); - - return { - xref: refTable, - rootRef, - root, - infoRef, - trailerStart, - previousXrefs: [], - xRefPosition, - }; -}; - -export default readPdf; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readPdf.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readPdf.min.js new file mode 100644 index 000000000..acfa4a753 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readPdf.min.js @@ -0,0 +1 @@ +import readRefTable from"./readRefTable.min.js";import findObject from"./findObject.min.js";const getValue=(e,t)=>{let r=e.indexOf(t);if(-1!==r)return e=e.slice(r),-1===(r=e.indexOf("/",1))&&(r=e.indexOf(">",1)),e.slice(t.length+1,r).toString().trim()},readPdf=e=>{var t=e.lastIndexOf("trailer"),r=e.slice(t,e.length-6),f=r.slice(r.lastIndexOf("startxref")+10).toString(),f=parseInt(f),i=readRefTable(e),n=getValue(r,"/Root");return{xref:i,rootRef:n,root:findObject(e,i,n).toString(),infoRef:getValue(r,"/Info"),trailerStart:t,previousXrefs:[],xRefPosition:f}};export default readPdf; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readRefTable.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readRefTable.js deleted file mode 100644 index 312f9b056..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readRefTable.js +++ /dev/null @@ -1,118 +0,0 @@ -import SignPdfError from './SignPdfError.js'; -import xrefToRefMap from './xrefToRefMap.js'; - -export const getLastTrailerPosition = (pdf) => { - const trailerStart = pdf.lastIndexOf(Buffer.from('trailer', 'utf8')); - const trailer = pdf.slice(trailerStart, pdf.length - 6); - - const xRefPosition = trailer - .slice(trailer.lastIndexOf(Buffer.from('startxref', 'utf8')) + 10) - .toString(); - - return parseInt(xRefPosition); -}; - -export const getXref = (pdf, position) => { - let refTable = pdf.slice(position); // slice starting from where xref starts - const realPosition = refTable.indexOf(Buffer.from('xref', 'utf8')); - if (realPosition === -1) { - throw new SignPdfError( - `Could not find xref anywhere at or after ${position}.`, - SignPdfError.TYPE_PARSE, - ); - } - if (realPosition > 0) { - const prefix = refTable.slice(0, realPosition); - if (prefix.toString().replace(/\s*/g, '') !== '') { - throw new SignPdfError( - `Expected xref at ${position} but found other content.`, - SignPdfError.TYPE_PARSE, - ); - } - } - - const nextEofPosition = refTable.indexOf(Buffer.from('%%EOF', 'utf8')); - if (nextEofPosition === -1) { - throw new SignPdfError( - 'Expected EOF after xref and trailer but could not find one.', - SignPdfError.TYPE_PARSE, - ); - } - refTable = refTable.slice(0, nextEofPosition); - refTable = refTable.slice(realPosition + 4); // move ahead with the "xref" - refTable = refTable.slice(refTable.indexOf('\n') + 1); // move after the next new line - - // extract the size - let size = refTable.toString().split('/Size')[1]; - - if (!size) { - throw new SignPdfError( - 'Size not found in xref table.', - SignPdfError.TYPE_PARSE, - ); - } - size = (/^\s*(\d+)/).exec(size); - if (size === null) { - throw new SignPdfError( - 'Failed to parse size of xref table.', - SignPdfError.TYPE_PARSE, - ); - } - size = parseInt(size[1]); - - const [objects, infos] = refTable.toString().split('trailer'); - - const isContainingPrev = infos.split('/Prev')[1] != null; - - let prev; - if (isContainingPrev) { - const pagesRefRegex = /Prev (\d+)/g; - const match = pagesRefRegex.exec(infos); - const [, prevPosition] = match; - prev = prevPosition; - } - - const xRefContent = xrefToRefMap(objects); - - return { - size, - prev, - xRefContent, - }; -}; - -export const getFullXrefTable = (pdf) => { - const lastTrailerPosition = getLastTrailerPosition(pdf); - const lastXrefTable = getXref(pdf, lastTrailerPosition); - - if (lastXrefTable.prev === undefined) { - return lastXrefTable.xRefContent; - } - const pdfWithoutLastTrailer = pdf.slice(0, lastTrailerPosition); - const partOfXrefTable = getFullXrefTable(pdfWithoutLastTrailer); - - const mergedXrefTable = new Map([ - ...partOfXrefTable, - ...lastXrefTable.xRefContent, - ]); - - return mergedXrefTable; -}; - -/** - * @param {Buffer} pdfBuffer - * @returns {object} - */ -const readRefTable = (pdf) => { - const fullXrefTable = getFullXrefTable(pdf); - const startingIndex = 0; - const maxIndex = Math.max(...fullXrefTable.keys()); - - return { - startingIndex, - maxIndex, - offsets: fullXrefTable, - }; -}; - -export default readRefTable; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readRefTable.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readRefTable.min.js new file mode 100644 index 000000000..1085bb067 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/readRefTable.min.js @@ -0,0 +1 @@ +import SignPdfError from"./SignPdfError.min.js";import xrefToRefMap from"./xrefToRefMap.min.js";const getLastTrailerPosition=e=>{var r=e.lastIndexOf(Buffer.from("trailer","utf8")),r=e.slice(r,e.length-6),e=r.slice(r.lastIndexOf(Buffer.from("startxref","utf8"))+10).toString();return parseInt(e)},getXref=(e,r)=>{let t=e.slice(r);e=t.indexOf(Buffer.from("xref","utf8"));if(-1===e)throw new SignPdfError(`Could not find xref anywhere at or after ${r}.`,SignPdfError.TYPE_PARSE);if(0{var r=getLastTrailerPosition(e),t=getXref(e,r);return void 0===t.prev?t.xRefContent:(e=e.slice(0,r),r=getFullXrefTable(e),new Map([...r,...t.xRefContent]))},readRefTable=e=>{e=getFullXrefTable(e);return{startingIndex:0,maxIndex:Math.max(...e.keys()),offsets:e}};export default readRefTable;export{getLastTrailerPosition,getXref,getFullXrefTable}; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/removeTrailingNewLine.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/removeTrailingNewLine.js deleted file mode 100644 index 0b5becfcb..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/removeTrailingNewLine.js +++ /dev/null @@ -1,42 +0,0 @@ -import SignPdfError from './SignPdfError.js'; - -const sliceLastChar = (pdf, character) => { - const lastChar = pdf.slice(pdf.length - 1).toString(); - if (lastChar === character) { - return pdf.slice(0, pdf.length - 1); - } - - return pdf; -}; - -/** - * Removes a trailing new line if there is such. - * - * Also makes sure the file ends with an EOF line as per spec. - * @param {Buffer} pdf - * @returns {Buffer} - */ -const removeTrailingNewLine = (pdf) => { - if (!(pdf instanceof Buffer)) { - throw new SignPdfError( - 'PDF expected as Buffer.', - SignPdfError.TYPE_INPUT, - ); - } - let output = pdf; - - output = sliceLastChar(output, '\n'); - output = sliceLastChar(output, '\r'); - - const lastLine = output.slice(output.length - 6).toString(); - if (lastLine !== '\n%%EOF') { - throw new SignPdfError( - 'A PDF file must end with an EOF line.', - SignPdfError.TYPE_PARSE, - ); - } - - return output; -}; - -export default removeTrailingNewLine; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/removeTrailingNewLine.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/removeTrailingNewLine.min.js new file mode 100644 index 000000000..88b4b06b2 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/removeTrailingNewLine.min.js @@ -0,0 +1 @@ +import SignPdfError from"./SignPdfError.min.js";const sliceLastChar=(r,e)=>{return r.slice(r.length-1).toString()===e?r.slice(0,r.length-1):r},removeTrailingNewLine=r=>{if(!(r instanceof Buffer))throw new SignPdfError("PDF expected as Buffer.",SignPdfError.TYPE_INPUT);r=sliceLastChar(r,"\n");if("\n%%EOF"!==(r=sliceLastChar(r,"\r")).slice(r.length-6).toString())throw new SignPdfError("A PDF file must end with an EOF line.",SignPdfError.TYPE_PARSE);return r};export default removeTrailingNewLine; \ No newline at end of file diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/xrefToRefMap.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/xrefToRefMap.js deleted file mode 100644 index 4b44019e6..000000000 --- a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/xrefToRefMap.js +++ /dev/null @@ -1,48 +0,0 @@ -import SignPdfError from './SignPdfError.js'; - -const xrefToRefMap = (xrefString) => { - const lines = xrefString.split('\n').filter((l) => l !== ''); - - let index = 0; - let expectedLines = 0; - const xref = new Map(); - lines.forEach((line) => { - const split = line.split(' '); - if (split.length === 2) { - index = parseInt(split[0]); - expectedLines = parseInt(split[1]); - return; - } - if (expectedLines <= 0) { - throw new SignPdfError( - 'Too many lines in xref table.', - SignPdfError.TYPE_PARSE, - ); - } - expectedLines -= 1; - const [offset, , inUse] = split; - if (inUse.trim() === 'f') { - index += 1; - return; - } - if (inUse.trim() !== 'n') { - throw new SignPdfError( - `Unknown in-use flag "${inUse}". Expected "n" or "f".`, - SignPdfError.TYPE_PARSE, - ); - } - if (!/^\d+$/.test(offset.trim())) { - throw new SignPdfError( - `Expected integer offset. Got "${offset}".`, - SignPdfError.TYPE_PARSE, - ); - } - const storeOffset = parseInt(offset.trim()); - xref.set(index, storeOffset); - index += 1; - }); - - return xref; -}; - -export default xrefToRefMap; diff --git a/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/xrefToRefMap.min.js b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/xrefToRefMap.min.js new file mode 100644 index 000000000..728676d86 --- /dev/null +++ b/apps/OpenSignServer/cloud/parsefunction/pdf/customSignPdf/xrefToRefMap.min.js @@ -0,0 +1 @@ +import SignPdfError from"./SignPdfError.min.js";const xrefToRefMap=r=>{r=r.split("\n").filter(r=>""!==r);let n=0,t=0;const f=new Map;return r.forEach(r=>{r=r.split(" ");if(2===r.length)n=parseInt(r[0]),t=parseInt(r[1]);else{if(t<=0)throw new SignPdfError("Too many lines in xref table.",SignPdfError.TYPE_PARSE);--t;var[r,,e]=r;if("f"!==e.trim()){if("n"!==e.trim())throw new SignPdfError(`Unknown in-use flag "${e}". Expected "n" or "f".`,SignPdfError.TYPE_PARSE);if(!/^\d+$/.test(r.trim()))throw new SignPdfError(`Expected integer offset. Got "${r}".`,SignPdfError.TYPE_PARSE);e=parseInt(r.trim());f.set(n,e)}n+=1}}),f};export default xrefToRefMap; \ No newline at end of file