ectd-ui/test.html
2026-09-15 10:54:51 +08:00

281 lines
9.3 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF 在线编辑器</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://unpkg.com/downloadjs@1.4.7"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.container {
max-width: 100%;
margin: 0 auto;
}
.button {
padding: 10px 20px;
margin: 10px 0;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.button:hover {
background-color: #45a049;
}
#pdfViewer {
width: 100%;
height: 80vh;
border: 1px solid #ccc;
margin-top: 20px;
position: relative;
overflow: auto;
}
#pdfCanvas {
display: block;
}
.link-input {
margin: 10px 0;
padding: 8px;
width: 100%;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
}
.modal-content {
background-color: white;
margin: 15% auto;
padding: 20px;
width: 50%;
border-radius: 5px;
}
.drawing-area {
position: absolute;
border: 2px dashed #000;
background-color: rgba(0,0,0,0.1);
pointer-events: none;
}
.controls {
display: flex;
gap: 10px;
margin-bottom: 10px;
}
.zoom-controls {
display: flex;
gap: 5px;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<h1>PDF 在线编辑器</h1>
<div class="controls">
<input type="file" id="pdfInput" accept=".pdf" />
<button class="button" onclick="loadPDF()">加载 PDF</button>
<button class="button" onclick="startDrawing()">绘制区域</button>
<button class="button" onclick="savePDF()">保存 PDF</button>
<div class="zoom-controls">
<button class="button" onclick="zoomOut()">-</button>
<span id="zoomLevel">100%</span>
<button class="button" onclick="zoomIn()">+</button>
</div>
</div>
<div id="pdfViewer">
<canvas id="pdfCanvas"></canvas>
</div>
</div>
<div id="linkModal" class="modal">
<div class="modal-content">
<h2>添加链接</h2>
<input type="text" id="linkUrl" class="link-input" placeholder="输入链接地址" />
<input type="text" id="linkText" class="link-input" placeholder="输入链接文本" />
<button class="button" onclick="addLink()">确定</button>
<button class="button" onclick="closeModal()">取消</button>
</div>
</div>
<script>
// 初始化 jsPDF
const { jsPDF } = window.jspdf;
let pdfDoc = null;
let pdfBytes = null;
let isDrawing = false;
let startX, startY;
let currentRect = null;
let selectedText = '';
let currentScale = 1.0;
let currentPage = null;
// 初始化 PDF.js
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.worker.min.js';
async function loadPDF() {
const fileInput = document.getElementById('pdfInput');
const file = fileInput.files[0];
if (!file) {
alert('请选择一个 PDF 文件');
return;
}
const arrayBuffer = await file.arrayBuffer();
pdfBytes = new Uint8Array(arrayBuffer);
// 使用 PDF.js 显示 PDF
const loadingTask = pdfjsLib.getDocument({data: pdfBytes});
const pdf = await loadingTask.promise;
currentPage = await pdf.getPage(1);
// 获取 PDF 页面的原始尺寸
const viewport = currentPage.getViewport({ scale: currentScale });
const canvas = document.getElementById('pdfCanvas');
const context = canvas.getContext('2d');
// 设置画布大小为 PDF 页面的原始尺寸
canvas.height = viewport.height;
canvas.width = viewport.width;
// 渲染 PDF 页面
await currentPage.render({
canvasContext: context,
viewport: viewport
}).promise;
// 更新缩放显示
document.getElementById('zoomLevel').textContent = Math.round(currentScale * 100) + '%';
// 添加文本选择事件
canvas.addEventListener('mouseup', handleTextSelection);
}
function zoomIn() {
currentScale += 0.1;
loadPDF();
}
function zoomOut() {
if (currentScale > 0.2) {
currentScale -= 0.1;
loadPDF();
}
}
function startDrawing() {
isDrawing = true;
const canvas = document.getElementById('pdfCanvas');
canvas.style.cursor = 'crosshair';
}
function handleTextSelection(e) {
if (isDrawing) {
if (!startX) {
startX = e.offsetX;
startY = e.offsetY;
currentRect = document.createElement('div');
currentRect.className = 'drawing-area';
currentRect.style.left = startX + 'px';
currentRect.style.top = startY + 'px';
document.getElementById('pdfViewer').appendChild(currentRect);
} else {
const width = e.offsetX - startX;
const height = e.offsetY - startY;
currentRect.style.width = Math.abs(width) + 'px';
currentRect.style.height = Math.abs(height) + 'px';
if (width < 0) currentRect.style.left = e.offsetX + 'px';
if (height < 0) currentRect.style.top = e.offsetY + 'px';
isDrawing = false;
startX = null;
startY = null;
document.getElementById('pdfCanvas').style.cursor = 'default';
// 显示链接输入弹窗
document.getElementById('linkModal').style.display = 'block';
}
}
}
function closeModal() {
document.getElementById('linkModal').style.display = 'none';
if (currentRect) {
currentRect.remove();
currentRect = null;
}
}
async function addLink() {
if (!currentRect) {
alert('请先绘制区域');
return;
}
const linkUrl = document.getElementById('linkUrl').value;
const linkText = document.getElementById('linkText').value;
if (!linkUrl) {
alert('请输入链接地址');
return;
}
try {
// 创建新的 jsPDF 文档
const doc = new jsPDF();
// 获取绘制区域的位置和大小
const rect = currentRect.getBoundingClientRect();
const canvas = document.getElementById('pdfCanvas');
const canvasRect = canvas.getBoundingClientRect();
// 计算 PDF 坐标(考虑缩放比例)
const scale = canvas.width / canvasRect.width;
const x = (rect.left - canvasRect.left) * scale;
const y = canvas.height - (rect.top - canvasRect.top) * scale;
const width = rect.width * scale;
const height = rect.height * scale;
// 添加链接文本(可选)
if (linkText) {
doc.text(linkText, x, y);
}
// 添加链接
doc.link(x, y, width, height, { url: linkUrl });
// 保存 PDF
const modifiedPdfBytes = doc.output('arraybuffer');
pdfBytes = new Uint8Array(modifiedPdfBytes);
closeModal();
await loadPDF(); // 重新加载 PDF 以显示更改
} catch (error) {
console.error('添加链接时出错:', error);
alert('添加链接失败: ' + error.message);
}
}
async function savePDF() {
if (!pdfBytes) {
alert('请先加载 PDF 文件');
return;
}
download(pdfBytes, "modified.pdf", "application/pdf");
}
</script>
</body>
</html>