Skip to content

Text missing after saving PDF #120

@ghufby

Description

@ghufby

What were you trying to do?

Open an existing PDF document with pdf-lib, make no changes, and then save it back to disk.

How did you attempt to do it?

I created a basic proof of concept in HTML + JavaScript that loads the PDF using PDFDocument.load(), and then immediately saves it using pdfDoc.save().

<!DOCTYPE html>
<html>
<head>
	<script src="./pdf-lib_2.5.2.js"></script>
</head>
<body>
	<input type="file" id="file" />
	<button id="save">Open & Save</button>
	<script type="module">
		const { PDFDocument } = PDFLib;
		document.getElementById('save').onclick = async () => {
			const fileInput = document.getElementById('file');
			if (fileInput.files.length) {
				const file = fileInput.files[0];
				const arrayBuffer = await file.arrayBuffer();
				// Load with pdf-lib
				const pdfDoc = await PDFDocument.load(arrayBuffer);
				// Serialize back to bytes
				const pdfBytes = await pdfDoc.save();
				// Trigger download
				const blob = new Blob([pdfBytes], { type: 'application/pdf' });
				const link = document.createElement('a');
				link.href = URL.createObjectURL(blob);
				link.download = 'normalized.pdf';
				link.click();
			}
		};
	</script>
</body>
</html>

What actually happened?

The PDF is saved, but all text disappears from the document. Images and other content remain intact, but embedded fonts are missing.

This issue only occurs with certain PDFs. Most PDFs I’ve tested work correctly. The attached test.pdf is an example that reproduces the problem.

What did you expect to happen?

I expected the output file to be visually identical to the input PDF (since no changes were made).

How can we reproduce the issue?

  • Open the attached PDF (test.pdf) with the provided HTML example (index.html).
  • Click "Open & Save".
  • Compare the downloaded normalized.pdf with the original input.
  • Notice that the text is missing from the output.

test.pdf

pdf-lib_2.5.2.js

index.html

Version

2.5.2 (same problem happens with 1.17.1)

What environment are you running pdf-lib in?

Browser

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions