Created
December 7, 2018 14:01
-
-
Save mathiasleroy/a982593e114d0edff95d3ee8ce53edbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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