Skip to content

Instantly share code, notes, and snippets.

@mathiasleroy
Created December 7, 2018 14:01
Show Gist options
  • Save mathiasleroy/a982593e114d0edff95d3ee8ce53edbd to your computer and use it in GitHub Desktop.
Save mathiasleroy/a982593e114d0edff95d3ee8ce53edbd to your computer and use it in GitHub Desktop.
// USING https://github.com/Hopding/pdf-lib
// ADD PDF TIMESTAMP AFTER ANCHOR CLICK
$(document).ready(function(){
$("a").click(function(event){
event.preventDefault();
console.log("user clicked pdf link");
file = event.target.href; // e.g. file = 'docs/example1.pdf';
fetch(file) .then(
function(response) {
console.log('Fetched!');
response.arrayBuffer().then(function(buffer) {
// INSERT TIMESTAMP ----------
const existingPdfDocBytes = new Uint8Array(buffer);
const pdfDoc = PDFDocumentFactory.load(existingPdfDocBytes);
const [helveticaFont] = pdfDoc.embedStandardFont('Helvetica');
const pages = pdfDoc.getPages();
const page = pages[0];
page.addFontDictionary('Helvetica', helveticaFont);
const contentStream = pdfDoc.createContentStream(
drawText( (new Date()).toISOString().slice(0, 19) , {
x: 200,
y: 800,
size: 16,
font: 'Helvetica',
colorRgb: [0.95, 0.26, 0.21],
})
);
page.addContentStreams(pdfDoc.register(contentStream));
const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);
// DOWNLOAD IMMEDIATLY ----------
download( pdfBytes, "textUInt8Array.pdf", "text/plain" );
});
}
).catch(function(err) {
console.log('Fetch Error :-S', err);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment