Copy Pages (pdf-lib)

Copy pages from existing documents with pdf-lib.

by Rapha Souza

HTML

<html>
  <head>
    <meta charset="utf-8" />
    <script src="https://unpkg.com/[email protected]"></script>
    <script src="https://unpkg.com/[email protected]"></script>
  </head>

  <body>
    <p>Click the button to copy pages from existing documents with <code>pdf-lib</code></p>
    <button onclick="copyPages()">Create PDF</button>
    <p class="small">(Your browser will download the resulting file)</p>
  </body>

  <script>
//const { PDFDocument } = PDFLib
//const { degrees, PDFDocument, rgb, StandardFonts } = PDFLib

    async function copyPages() {
     const { degrees, PDFDocument, rgb, StandardFonts } = PDFLib
      import { PageSizes } from 'pdf-lib'

  const url = 'https://lab.fluig.com/volume/stream/Rmx1aWc=/P3Q9MSZ2b2w9RGVmYXVsdCZpZD01MTMxJnZlcj0xMDAwJmZpbGU9VGVybW8rZGUrUmVjZWJpbWVudG8rRVQtMDAwMDEtQ05UKy0rRUIrVEVTVEUrMDAzLUFETS5wZGYmY3JjPTExODg5ODQ1OTgmc2l6ZT0wLjE2NTg0NSZ1SWQ9MyZmU0lkPTEmdVNJZD0xJmQ9ZmFsc2UmdGtuPSZwdWJsaWNVcmw9dHJ1ZQ==.pdfhttps://lab.fluig.com:443/volume/stream/Rmx1aWc=/P3Q9MSZ2b2w9RGVmYXVsdCZpZD0xNDEmdmVyPTEwMDAmZmlsZT1Db25zdWx0YStTRVJBU0EucGRmJmNyYz0yNzU5NDg4NzE5JnNpemU9MC40NDE4MyZ1SWQ9MyZmU0lkPTEmdVNJZD0xJmQ9ZmFsc2UmdGtuPSZwdWJsaWNVcmw9dHJ1ZQ==.pdf'
  const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer())

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

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

  // Get the first page of the document
  const pages = pdfDocSign.getPages()
  const firstPage = pages[0]
  const lastPageCount = pages.length - 1
  const lastPage = pages[lastPageCount]
  console.table(lastPage)

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

  var dataAtual = new Date()
  var mesPtBR = new Intl.DateTimeFormat('pt-BR', { day: 'numeric', month: 'long', year: 'numeric' }).format(dataAtual);
  var horaAtual =...

CSS

body {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

p {
  font-family: helvetica;
  font-size: 24px;
  text-align: center;
  margin: 25px;
}

.small {
  font-family: helvetica;
  font-size: 18px;
  text-align: center;
  margin: 25px;
}

button {
  background-color: #008CBA;
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  font-size: 16px;
}