Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
7.02k stars 676 forks source link

External links work but link to same page doesn't work #1609

Open rajashree23 opened 8 months ago

rajashree23 commented 8 months ago

What were you trying to do?

I am trying to add links to the same page, however when Inam trying to merge the PDfs it is getting lost unlike external links,

How did you attempt to do it?

What actually happened?

The external links on the page are working fine but the links to the pages are not working

What did you expect to happen?

I want a way to tag internal pages as well

How can we reproduce the issue?

  1. Creating different PDFs from puppeteer
const fs = require('fs');
const puppeteer = require("puppeteer");
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<a href="https://www.google.fr/maps/@50.6288598,3.0621533,14z">Google.fr maps</a>

    <ul>
<li><a href="#test1">Test 1</a></li>
<li><a href="#test2">Test 2</a></li>
<li><a href="#test3">Test 3</a></li>
<li><a href="#test4">Test 4</a></li>
<li><a href="#test5">Test 5</a></li>
</ul>

    <h1 id="test1" style="margin-bottom: 500px">Test 1 title</h1>
<h1 id="test2" style="margin-bottom: 500px">Test 2 title</h1>
<h1 id="test3" style="margin-bottom: 500px">Test 3 title</h1>
<h1 id="test4" style="margin-bottom: 500px">Test 4 title</h1>
<h1 id="test5" style="margin-bottom: 500px">Test 5 title</h1>
</body>
</html>`;
(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    devtools: true,
    args: [
      "--disable-web-security",
      "--disable-features=IsolateOrigins",
      "--disable-site-isolation-trials",
    ],
  });
  const page1 = await browser.newPage();

  await page1.setContent(html.toString("utf-8"));
  const pdfBuffer1 = await page1.pdf();
  const writeStream1 = fs.createWriteStream("./example1.pdf");
  writeStream1.write(pdfBuffer1);
  writeStream1.close();

  const page2 = await browser.newPage();
  await page2.setContent(html.toString("utf-8"));
  const pdfBuffer2 = await page2.pdf();
  const writeStream2 = fs.createWriteStream("./example2.pdf");
  writeStream2.write(pdfBuffer2);
  writeStream2.close();

  await browser.close();
})();
  1. merge PDFs
(async () => {
  const mergedPdf = await PDFDocument.create();

  const pdfA = await PDFDocument.load(fs.readFileSync("example1.pdf"));
  const pdfB = await PDFDocument.load(fs.readFileSync("example1.pdf"));

  const copiedPagesA = await mergedPdf.copyPages(pdfA, pdfA.getPageIndices());
  copiedPagesA.forEach((page) => mergedPdf.addPage(page));

  const copiedPagesB = await mergedPdf.copyPages(pdfB, pdfB.getPageIndices());
  copiedPagesB.forEach((page) => mergedPdf.addPage(page));

  const mergedPdfFile = await mergedPdf.save();

  const writeStream = fs.createWriteStream("./final.pdf");
  writeStream.write(mergedPdfFile);
  writeStream.close();
})();

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

Kindly help me out as this is urgent!