如何在 JavaScript 中将网页 URL 的内容下载为 pdf 而不打开链接?

问题描述 投票:0回答:1

我在另一个页面上有一个下载个人资料按钮,如果我单击该按钮,它必须将我的个人资料页面内容下载为 PDF,特别是类名“.resume”的内容。我已经使用 html2pdf 库实现了相同页面下载选项,但不知道如何从不同页面下载。请帮助我,谢谢。

同页下载码:

const downloadPDF = async () => {
    
      const resume = document.querySelector('.resume');
      toHide.value = 'none'
    
      await html2pdf()
        .set({filename: 'resume.pdf'})
        .from(resume)
        .save()
    
      toHide.value = 'inline'
    };
javascript pdf vuejs3 pdf-generation quasar-framework
1个回答
0
投票

既然是同源,你可以fetch页面并提取简历

类似的东西

const downloadPDF = async() => {
  toHide.value = 'none';
  // Fetch the HTML content of the resume page
  fetch('/resume-page')
    .then(response => response.text())
    .then(htmlContent => {
      // Create a temporary div (fragment) to hold the HTML content
      const tempDiv = document.createElement('div');
      tempDiv.innerHTML = htmlContent;
      // Extract the resume element
      const resume = tempDiv.querySelector('.resume');
      // Generate PDF
      return html2pdf()
        .set({
          filename: 'resume.pdf'
        })
        .from(resume)
        .save();
    })
    .catch(error => {
      console.error("Something went wrong: ", error);
    })
    .finally(() => {
      // Restore the state 
      toHide.value = 'inline';
    });
};
© www.soinside.com 2019 - 2024. All rights reserved.