跳转至

关于拜登的俄罗斯出生的货币审计长提名人索尔·奥马罗娃,我们需要了解什么

console.log('download-pdf.js loaded'); // 调试信息

function downloadPDF() {
console.log('Download button clicked'); // 调试信息
const title = document.title
.replace(/[^\u4e00-\u9fa5a-zA-Z0-9\s]/g, '-') // 替换特殊字符为连字符
.replace(/\s+/g, '-'); // 替换空格为连字符

const element = document.querySelector('.md-content'); // 选择 MkDocs 内容区域

// 获取内容的实际宽度和高度
const contentWidth = element.scrollWidth;
const contentHeight = element.scrollHeight;

// 配置 html2canvas
const options = {
    margin: 0, // 去除边距
    filename: `${title}.pdf`, // 使用清理后的标题作为文件名
    image: { type: 'jpeg', quality: 0.98 },
    html2canvas: {
        scale: 2, // 提高分辨率
        useCORS: true, // 允许跨域图片
        logging: true, // 开启日志(调试用)
        scrollY: 0, // 禁止滚动
        width: contentWidth, // 设置画布宽度为内容宽度
        height: contentHeight, // 设置画布高度为内容高度
        windowWidth: contentWidth, // 设置窗口宽度为内容宽度
        windowHeight: contentHeight, // 设置窗口高度为内容高度
        onclone: (clonedDoc) => {
            // 在克隆的文档中移除可能导致分页的元素
            clonedDoc.querySelectorAll('.page-break').forEach(el => el.remove());
        }
    },
    jsPDF: {
        unit: 'px', // 使用像素单位
        format: [contentWidth, contentHeight], // 设置 PDF 尺寸为内容尺寸
        orientation: 'portrait',
        putOnlyUsedFonts: true, // 仅嵌入使用的字体
        compress: true // 压缩 PDF
    }
};

// 获取图片背后的隐藏链接(即 <img> 标签的父级 <a> 标签)
const links = [];
element.querySelectorAll('a').forEach(link => {
    // 检查链接是否包含 <img> 标签
    if (link.querySelector('img')) {
        const rect = link.getBoundingClientRect();
        if (rect.width > 0 && rect.height > 0) { // 只处理可见的链接
            links.push({
                url: link.href,
                x: rect.left,
                y: rect.top,
                width: rect.width,
                height: rect.height
            });
        }
    }
});

// 使用 html2pdf 生成 PDF
html2pdf()
    .from(element)
    .set(options)
    .toPdf()
    .get('pdf')
    .then((pdf) => {
        // 在 PDF 中添加链接
        links.forEach(link => {
            pdf.link(link.x, link.y, link.width, link.height, { url: link.url });
        });
        pdf.save(options.filename); // 保存 PDF
    });

}

document.addEventListener('DOMContentLoaded', function () {
console.log('DOM fully loaded'); // 调试信息
const button = document.createElement('button');
button.textContent = '下载本页为 PDF'; // 按钮显示为中文
button.style.position = 'fixed';
button.style.bottom = '20px';
button.style.right = '20px';
button.style.zIndex = 1000;
button.style.padding = '10px 20px';
button.style.backgroundColor = '#0078d4';
button.style.color = '#fff';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
button.onclick = downloadPDF;

document.body.appendChild(button);
console.log('按钮已添加到页面'); // 调试信息

});

引用:https://www.theorganicprepper.com/saule-omarova-nomination/
原文: https://s2.tttl.online/blog/1735905025/

图书推荐