Files
data-index-report/test-pdf.html
2026-05-18 13:52:47 +08:00

77 lines
2.5 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/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
.test-content {
width: 794px;
min-height: 1123px;
background: white;
border: 1px solid #ccc;
padding: 40px;
}
button {
margin: 20px;
padding: 10px 20px;
font-size: 16px;
background: #3b82f6;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background: #2563eb;
}
</style>
</head>
<body>
<h1>PDF 生成测试</h1>
<button onclick="generatePDF()">生成测试 PDF</button>
<div id="content" class="test-content">
<h2>测试内容</h2>
<p>这是一个简单的测试内容,用于验证 html2pdf.js 是否正常工作。</p>
<p>当前时间: <span id="time"></span></p>
<img src="https://via.placeholder.com/300x200" alt="测试图片">
</div>
<script>
document.getElementById('time').textContent = new Date().toLocaleString();
function generatePDF() {
const element = document.getElementById('content');
const opt = {
margin: 0,
filename: 'test-report.pdf',
image: { type: 'jpeg', quality: 0.98 },
enableLinks: true,
html2canvas: {
scale: 2,
useCORS: true,
logging: true,
width: 794,
windowWidth: 794,
scrollY: 0,
letterRendering: true,
backgroundColor: '#ffffff',
},
jsPDF: { unit: 'px', format: [794, 1123], orientation: 'portrait' },
pagebreak: { mode: ['avoid-all', 'css', 'legacy'] }
};
console.log('开始生成 PDF...');
html2pdf().set(opt).from(element).save()
.then(() => console.log('PDF 生成成功'))
.catch(err => console.error('PDF 生成失败:', err));
}
</script>
</body>
</html>