Fetch the repository succeeded.
This action will force synchronization from Youreln/Youreln工具箱, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
<!DOCTYPE html>
<html>
<head>
<title>HTML转PDF工具</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.0/jspdf.umd.min.js"></script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
</head>
<body>
<h1>HTML转PDF工具</h1>
<form id="form">
<label for="url">输入网址:</label>
<input type="text" id="url" name="url" placeholder="https://example.com" required>
<button onclick="exportPdfFromUrl()">导出PDF</button>
</form>
<h2>或者</h2>
<form id="uploadForm">
<label for="htmlFile">上传HTML文件:</label>
<input type="file" id="htmlFile" name="htmlFile" accept=".html">
<button onclick="exportPdfFromFile()">导出PDF</button>
</form>
<script>
function exportPdfFromUrl() {
const url = document.getElementById("url").value;
if (url.trim() === "") {
alert("请输入有效的网址!");
return;
}
const proxyUrl = "https://cors-anywhere.herokuapp.com/"; // 使用代理解决跨域问题
const apiUrl = proxyUrl + url;
fetch(apiUrl)
.then(response => response.text())
.then(html => {
exportPdf(html);
})
.catch(error => {
alert("无法获取网页内容:" + error);
});
}
function exportPdfFromFile() {
const fileInput = document.getElementById("htmlFile");
const file = fileInput.files[0];
if (!file) {
alert("请选择一个HTML文件!");
return;
}
const reader = new FileReader();
reader.onload = function(event) {
const html = event.target.result;
exportPdf(html);
};
reader.readAsText(file);
}
function exportPdf(html) {
const div = document.createElement("div");
div.innerHTML = html;
html2canvas(div).then((canvas) => {
const imgData = canvas.toDataURL("image/png");
const pdf = new jsPDF();
const imgProps = pdf.getImageProperties(imgData);
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save("output.pdf");
});
}
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。