MVP - Scale Pages, Scale Content
This commit is contained in:
29
public/functions/scaleContent.js
Normal file
29
public/functions/scaleContent.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const { PDFDocument, ParseSpeeds } = PDFLib;
|
||||
|
||||
export const scaleContent = async (snapshot, scale_factor) => {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFDocument.load(snapshot, {
|
||||
parseSpeed: ParseSpeeds.Fastest,
|
||||
});
|
||||
|
||||
const pages = pdfDoc.getPages();
|
||||
|
||||
pages.forEach(page => {
|
||||
const width = page.getWidth();
|
||||
const height = page.getHeight();
|
||||
|
||||
// Scale content
|
||||
page.scaleContent(scale_factor, scale_factor);
|
||||
const scaled_diff = {
|
||||
width: Math.round(width - scale_factor * width),
|
||||
height: Math.round(height - scale_factor * height),
|
||||
};
|
||||
|
||||
// Center content in new page format
|
||||
page.translateContent(Math.round(scaled_diff.width / 2), Math.round(scaled_diff.height / 2));
|
||||
|
||||
});
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
};
|
||||
31
public/functions/scalePage.js
Normal file
31
public/functions/scalePage.js
Normal file
@@ -0,0 +1,31 @@
|
||||
const { PDFDocument, ParseSpeeds } = PDFLib;
|
||||
|
||||
export const scalePage = async (snapshot, page_size) => {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFDocument.load(snapshot, {
|
||||
parseSpeed: ParseSpeeds.Fastest,
|
||||
});
|
||||
|
||||
const new_size = page_size;
|
||||
|
||||
const pages = pdfDoc.getPages();
|
||||
|
||||
pages.forEach(page => {
|
||||
// Change page size
|
||||
page.setSize(new_size.width, new_size.height);
|
||||
});
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
};
|
||||
|
||||
export const PageSize = {
|
||||
a4: {
|
||||
width: 594.96,
|
||||
height: 841.92
|
||||
},
|
||||
letter: {
|
||||
width: 612,
|
||||
height: 792
|
||||
}
|
||||
};
|
||||
0
public/index.css
Normal file
0
public/index.css
Normal file
17
public/index.html
Normal file
17
public/index.html
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
|
||||
<script src="https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js"></script>
|
||||
<script src="https://unpkg.com/downloadjs@1.4.7"></script>
|
||||
|
||||
<script src="index.js" type="module"></script>
|
||||
</head>
|
||||
<body>
|
||||
<input type="file" id="pdfFile" accept=".pdf">
|
||||
<a id="downloadLink" style="display: none;" download="modified.pdf">Download Modified PDF</a>
|
||||
</body>
|
||||
</html>
|
||||
16
public/index.js
Normal file
16
public/index.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { scaleContent } from "./functions/scaleContent.js";
|
||||
import { scalePage, PageSize } from "./functions/scalePage.js";
|
||||
|
||||
(async () => {
|
||||
const pdfFileInput = document.getElementById('pdfFile');
|
||||
|
||||
pdfFileInput.addEventListener('change', async (e) => {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
let pdfBuffer = new Uint8Array(await file.arrayBuffer());
|
||||
pdfBuffer = await scaleContent(pdfBuffer, 2);
|
||||
pdfBuffer = await scalePage(pdfBuffer, PageSize.letter);
|
||||
download(pdfBuffer, "pdf-lib_creation_example.pdf", "application/pdf");
|
||||
}
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user