网页变成PDF下载到本地

开发过程中如何让整个网页 或者 网页中某一部分支持下载成pdf格式文件



以vue 为例 html2canvas

1.安装好 html2canvas 和 jspdf

2.页面引入

js 复制代码
<template>
  <div class="contract-form">
   xxxxxxx
   xxxxx
   xxxxx
   <button @click="downloadAsPDF" class="download-button">下载为PDF</button>
   </div>
 </template>  

import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';

<script>


downloadAsPDF() {
      const element = document.querySelector('.contract-form');
      const button = document.querySelector('.download-button');

      // 临时隐藏下载按钮
      button.style.display = 'none';

      // 临时缩小内容样式
      const originalStyles = {
        fontSize: element.style.fontSize,
        padding: element.style.padding,
        maxWidth: element.style.maxWidth
      };
      element.style.fontSize = '1px';
      element.style.padding = '30px';
      element.style.maxWidth = '1000px';

      const options = {
        scale: 2, // 保持高清晰度
        useCORS: true,
        allowTaint: true,
        logging: true,
        scrollX: 0,
        scrollY: 0,
        windowWidth: element.scrollWidth,
        windowHeight: element.scrollHeight,
        ignoreElements: (el) => {
          return false;
        },
        onclone: (clonedDoc) => {
          // 确保所有内容在渲染时可见
          clonedDoc.querySelector('.contract-form').style.overflow = 'visible';
        }
      };

      // 临时调整页面布局以确保所有内容可见
      const originalOverflow = element.style.overflow;
      element.style.overflow = 'visible';

      html2canvas(element, options).then(canvas => {
        const imgData = canvas.toDataURL('image/png');
        const pdf = new jsPDF('p', 'mm', 'a4');
        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('委托检验合同书.pdf');

        // 恢复原始布局
        element.style.overflow = originalOverflow;
        button.style.display = 'block';
      });
    }

根据自己的需求 把对应的DOM 和值设置好直接可以生成pdf

功能 - 了解 html2canvas 支持的不同功能

以下是所有支持的 CSS 属性和值的列表。

复制代码
background
background-clip (不支持 text)
background-color
background-image
url()
linear-gradient()
radial-gradient()
background-origin
background-position
background-size
border
border-color
border-radius
border-style (只支持 solid)
border-width
bottom
box-sizing
content
color
display
flex
float
font
font-family
font-size
font-style
font-variant
font-weight
height
left
letter-spacing
line-break
list-style
list-style-image
list-style-position
list-style-type
margin
max-height
max-width
min-height
min-width
opacity
overflow
overflow-wrap
padding
position
right
text-align
text-decoration
text-decoration-color
text-decoration-line
text-decoration-style (只支持 solid)
text-shadow
text-transform
top
transform (Limited support)
visibility
white-space
width
word-break
word-spacing
word-wrap
z-index

不支持的 CSS 属性 这些 CSS 属性当前版本还不支持

复制代码
background-blend-mode
border-image
box-decoration-break
box-shadow
filter
font-variant-ligatures
mix-blend-mode
object-fit
repeating-linear-gradient()
writing-mode
zoom
相关推荐
Autumn_ing3 小时前
Design.md生成UI怎么验收?举例运营后台的颜色、状态和数据核对
前端·ui·ai·设计系统·paico·ai生成ui·design.md
远离UE44 小时前
UE5 Substrate Thin Film 源码拆析
开发语言·javascript·ue5
keyipatience4 小时前
JavaScript学习(1)书写位置,变量、数据类型、运算符
开发语言·前端·javascript·css·学习
朱涛的自习室4 小时前
我给 Gemini 4 和 DeepSeek 搞了套「末位淘汰」,AI 干活会更猛吗?(Day 3)
android·前端·后端
java1234_小锋6 小时前
【技术专题】HTML5 网页技术 - HTML5简介 & HTML5 文档结构
前端·html·html5
鶴哥只手遮天7 小时前
Vue 3 核心源码深度剖析系列(v3.5.44)-第 1 章 项目全景:monorepo 架构与工程化体系
前端·vue.js·开源
流星。。方7 小时前
小团队官网SEO落地:前端语义化与静态生成的工程实践
前端·状态模式
用户530429765797 小时前
盘点危化品搬运诊断的6类工具与自处理边界判断
前端
curtain7 小时前
从古法编程到 Agent:我入行这两年,AI 改变了什么
前端·openai
zyfts7 小时前
10分钟自制Codex钉钉通知Skill:AI干完活自动钉钉提醒你
前端