PDFlib 添加密码?

问题描述

PDFlib 是否可以添加用户和所有者密码?我在您的网站上看到是这样,但我不知道如何添加代码添加密码的选项?

我以 base64 格式保存 pdf,并使用这个库在每张纸的边缘添加一个文本。但如果无法编辑,我想有可能用密码保护它。

    const { degrees,PDFDocument,rgb,StandardFonts } = PDFLib
modifyPdf();
    async function modifyPdf() {
      // Fetch an existing PDF document
      const url = '<?PHP echo $pdf;?>'
        const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer())

      // Load a PDFDocument from the existing PDF bytes
      const pdfDoc = await PDFDocument.load(existingPdfBytes)

      // Embed the Helvetica font
      const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica)

// Get document pages
const pages = pdfDoc.getPages()

// Iterate every pages
pages.forEach(page => {
  
  // Get the width and height of the page
  const {
    width,height
  } = page.getSize()

  // Draw a string of text diagonally across the page
  page.drawText('Firmado por <?PHP echo $usuarionombre;?>,en fecha <?PHP echo $fechafirma;?>!',{
    x: 12,y: height / 2 - 300,size: 12,font: helveticaFont,color: rgb(0.95,0.1,0.1),rotate: degrees(90),})
})




      // Serialize the PDFDocument to bytes (a Uint8Array)
      const pdfBytes = await pdfDoc.save()

            // esto es para descargar el pdf
      //download(pdfBytes,"pdf-lib_modification_example.pdf","application/pdf");
      // This step is only necessary if you don't already have a Buffer Object


var blob = new Blob([pdfBytes])

var reader = new FileReader();
reader.onload = function(event){
   var base64 =   event.target.result
   var res = base64.replace("data:application/octet-stream;base64,","");
   var sites = ['data:application/pdf;base64,'+res]
    
    document.getElementById('myIframe').src = sites[Math.floor(Math.random() * sites.length)];
   
};

reader.readAsDataURL(blob);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...