首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在PDF的边上为每一页写入文本

在PDF的边上为每一页写入文本
EN

Stack Overflow用户
提问于 2021-04-01 05:46:11
回答 1查看 192关注 0票数 0

我使用pdflib,它非常适合我。但是我不能让它在PDF的所有页面上写一条消息。

如果我上传了一个7页的PDF,我如何才能使该消息出现在所有7页上。如果我上传一个5页的PDF,它会出现在所有5页上。

我试过使用if,但最后还是不能使用它。

代码语言:javascript
运行
复制
<script>
  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 the first page of the document
    const pages = pdfDoc.getPages()

    const firstPage = pages[0]
    // Get the width and height of the first page
    const {
      width,
      height
    } = firstPage.getSize()

    // Draw a string of text diagonally across the first page
    firstPage.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),
      //original ajustes.
      // x: 5,
      //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);
  }
</script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-01 21:44:50

不确定这是否是您试图在所有页面上编写的“对角线消息”,但如果是这样的话,您应该在所有pages上迭代,以便在每个页面上使用drawText(),而不是只获得第一个页面的const firstPage = pages[0]

代码语言:javascript
运行
复制
// 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),
  })
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66895749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档