当前位置:首页 > React

react如何实现打印pdf功能

2026-01-25 05:16:36React

使用react-to-print库

安装react-to-print库:

npm install react-to-print

创建一个可打印的组件:

import React from 'react';
import { useReactToPrint } from 'react-to-print';

class ComponentToPrint extends React.Component {
  render() {
    return (
      <div>
        <h1>打印内容标题</h1>
        <p>这是要打印的内容</p>
      </div>
    );
  }
}

const Example = () => {
  const componentRef = React.useRef();
  const handlePrint = useReactToPrint({
    content: () => componentRef.current,
  });

  return (
    <div>
      <ComponentToPrint ref={componentRef} />
      <button onClick={handlePrint}>打印</button>
    </div>
  );
};

使用PDF库生成PDF

安装pdf-lib库:

npm install pdf-lib

创建并下载PDF文件:

import { PDFDocument, StandardFonts, rgb } from 'pdf-lib';

const generatePdf = async () => {
  const pdfDoc = await PDFDocument.create();
  const page = pdfDoc.addPage();
  const { width, height } = page.getSize();

  const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
  page.drawText('Hello World!', {
    x: 50,
    y: height - 50,
    size: 30,
    font,
    color: rgb(0, 0, 0),
  });

  const pdfBytes = await pdfDoc.save();
  const blob = new Blob([pdfBytes], { type: 'application/pdf' });
  const url = URL.createObjectURL(blob);

  const link = document.createElement('a');
  link.href = url;
  link.download = 'document.pdf';
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};

使用浏览器打印API

直接调用浏览器打印功能:

const handlePrint = () => {
  window.print();
};

// 在组件中
<button onClick={handlePrint}>打印页面</button>

样式调整优化打印效果

添加打印样式表:

@media print {
  body * {
    visibility: hidden;
  }
  .print-container, .print-container * {
    visibility: visible;
  }
  .print-container {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
  }
  .no-print {
    display: none;
  }
}

使用html2canvas和jspdf组合

安装依赖:

npm install html2canvas jspdf

实现截图转PDF:

react如何实现打印pdf功能

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

const exportToPDF = async (elementId, fileName) => {
  const element = document.getElementById(elementId);
  const canvas = await html2canvas(element);
  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(`${fileName}.pdf`);
};

分享给朋友:

相关文章

vue如何实现单选

vue如何实现单选

Vue 实现单选的方法 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定到同一个变量,确保同一时间只有一个选…

vue如何实现目录组件

vue如何实现目录组件

实现目录组件的基本思路 在Vue中实现目录组件通常需要结合页面内容的结构化数据(如标题层级),通过动态渲染生成可交互的目录。核心步骤包括提取标题、生成目录结构、实现滚动联动等。 提取标题信息 通过d…

php如何实现直播

php如何实现直播

实现直播功能的方法 PHP可以通过结合其他技术和工具来实现直播功能。以下是几种常见的方法: 使用流媒体服务器 配置流媒体服务器如Nginx-RTMP、Red5或Wowza。这些服务器支持RTMP协…

vue如何实现登录

vue如何实现登录

Vue 实现登录功能的方法 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,通常命名为 Login.vue。表单包含用户名和密码输入框,以及提交按钮。 <template>…

vue功能实现难点

vue功能实现难点

Vue 功能实现的常见难点 响应式系统深度监听 Vue 的响应式系统基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3),但对嵌套对象或数组的深层属性变化需要额外…

php购物车功能实现

php购物车功能实现

数据库设计 购物车功能通常需要设计数据库表来存储商品和用户信息。常见的表包括: products 表:存储商品信息,如商品ID、名称、价格、库存等。 users 表:存储用户信息,如用户ID、用户名…