mirror of
https://github.com/OpenSignLabs/OpenSign.git
synced 2026-08-22 07:32:31 +02:00
initial commit
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user