简体   繁体   English

Node 中 PDF 的到期日期

[英]Expiry date of PDFs in Node

I'm trying to generate a PDF with an expiry date (auto expiring after 24 hours).我正在尝试生成带有到期日期的 PDF(24 小时后自动到期)。

I have strong experience with pdf-lib , but searching through their repo there is no mention of expiry dates.我对pdf-lib有丰富的经验,但在他们的回购中搜索没有提到到期日期。

I've also found two articles on how to do it in C# and Python:我还找到了两篇关于如何在 C# 和 Python 中执行此操作的文章:

  1. C#/VB: https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Security/How-to-Add-Expiry-Date-to-PDF-Files-in-C-VB.NET.html C#/VB: https : //www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Security/How-to-Add-Expiry-Date-to-PDF-Files-in- C-VB.NET.html
  2. https://docs.aspose.com/pdf/java/set-pdf-expiration-in-python/ https://docs.aspose.com/pdf/java/set-pdf-expiration-in-python/

I'm trying to make a PDF expire on a person's device after they have downloaded it from the server and surprised to see there isn't much support in the node/pdf area.我试图让一个人的设备上的 PDF 在他们从服务器下载后过期,并且惊讶地发现 node/pdf 区域没有太多支持。

Any suggestions?有什么建议? Is this possible?这可能吗?

import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'

const pdfDoc = await PDFDocument.create()
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
const page = pdfDoc.addPage()
const { width, height } = page.getSize()
const fontSize = 30
page.drawText('Creating PDFs in JavaScript would be great if the PDF had an expiry date!', {
  x: 50,
  y: height - 4 * fontSize,
  size: fontSize,
  font: timesRomanFont,
  color: rgb(0, 0.53, 0.71),
})

// TODO: add expiry before the save.

const pdfBytes = await pdfDoc.save()

You can use addJavascript method, so your code will look like:您可以使用addJavascript方法,因此您的代码将如下所示:

 import { PDFDocument, StandardFonts, rgb } from 'pdf-lib' const pdfDoc = await PDFDocument.create() const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman) pdfDoc.addJavaScript( 'main', 'var year=2020; var month=11;today = new Date();today = new Date(today.getFullYear(), today.getMonth());expiry = new Date(year, month);if (today.getTime() > expiry.getTime())app.alert("The file is expired. You need a new one.");', ); const page = pdfDoc.addPage() const { width, height } = page.getSize() const fontSize = 30 page.drawText('Creating PDFs in JavaScript would be great if the PDF had an expiry date!', { x: 50, y: height - 4 * fontSize, size: fontSize, font: timesRomanFont, color: rgb(0, 0.53, 0.71), }) const pdfBytes = await pdfDoc.save()

You can more investigate it there https://github.com/Hopding/pdf-lib/commit/30d2aa22c0c0d694189ae3202562d4c0565cce42您可以在那里进行更多调查https://github.com/Hopding/pdf-lib/commit/30d2aa22c0c0d694189ae3202562d4c0565cce42

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM