115 lines
4.2 KiB
HTML
Raw Normal View History

2025-03-07 18:26:36 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Page Extractor and Cover Generator</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf-lib/1.17.1/pdf-lib.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.10.377/pdf.min.js"></script>
<style>
#pdf-render {
max-width: 100%;
height: auto;
border: 1px solid #ddd;
margin-top: 20px;
}
</style>
</head>
<body>
<h2>Upload a PDF and Extract Specific Pages</h2>
<input type="file" id="pdfFile" accept="application/pdf">
<br><br>
<label for="startPage">Start Page:</label>
<input type="number" id="startPage" min="1">
<label for="endPage">End Page:</label>
<input type="number" id="endPage" min="1">
<br><br>
<button onclick="extractPages()">Extract Pages</button>
<br><br>
<a id="downloadLink" style="display: none;">Download Extracted PDF</a>
<h2>Generate PDF Cover Page</h2>
<input type="file" id="pdf-upload" accept="application/pdf">
<canvas id="pdf-render"></canvas>
<script>
async function extractPages() {
const fileInput = document.getElementById('pdfFile');
const startPage = parseInt(document.getElementById('startPage').value);
const endPage = parseInt(document.getElementById('endPage').value);
if (!fileInput.files.length) {
alert('Please upload a PDF file.');
return;
}
if (isNaN(startPage) || isNaN(endPage) || startPage <= 0 || endPage < startPage) {
alert('Please enter a valid page range.');
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const pdfDoc = await PDFLib.PDFDocument.load(arrayBuffer);
const totalPages = pdfDoc.getPageCount();
if (startPage > totalPages || endPage > totalPages) {
alert('Page range exceeds the total number of pages in the PDF.');
return;
}
const newPdfDoc = await PDFLib.PDFDocument.create();
for (let i = startPage - 1; i < endPage; i++) {
const [copiedPage] = await newPdfDoc.copyPages(pdfDoc, [i]);
newPdfDoc.addPage(copiedPage);
}
const pdfBytes = await newPdfDoc.save();
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'extracted_pages.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Extracted PDF';
}
document.getElementById('pdf-upload').addEventListener('change', async function(event) {
const file = event.target.files[0];
if (file && file.type === "application/pdf") {
const fileReader = new FileReader();
fileReader.onload = async function() {
const typedArray = new Uint8Array(this.result);
const loadingTask = pdfjsLib.getDocument(typedArray);
try {
const pdf = await loadingTask.promise;
const page = await pdf.getPage(1); // 获取第一页
const viewport = page.getViewport({ scale: 1.5 });
const canvas = document.getElementById('pdf-render');
const context = canvas.getContext('2d');
canvas.width = viewport.width;
canvas.height = viewport.height;
// 渲染页面
const renderContext = {
canvasContext: context,
viewport: viewport
};
await page.render(renderContext).promise;
console.log('封面生成成功');
} catch (error) {
console.error('无法加载 PDF 文件:', error);
}
};
fileReader.readAsArrayBuffer(file);
} else {
alert('请选择一个有效的 PDF 文件');
}
});
</script>
</body>
</html>