当前位置:首页 > 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库:

react如何实现打印pdf功能

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

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

react如何实现打印pdf功能

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:

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 中实现分页功能通常需要结合后端 API 或前端数据分页逻辑。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在 Vue 组件中处理分页逻辑…

vue如何实现放大缩小

vue如何实现放大缩小

Vue 实现放大缩小功能 在 Vue 中实现放大缩小功能可以通过多种方式实现,以下介绍几种常见的方法: 使用 CSS transform 缩放 通过绑定 CSS 的 transform: scal…

vue实现产品使用功能

vue实现产品使用功能

Vue 实现产品功能的关键方法 组件化开发 使用Vue的单文件组件(.vue)将产品功能模块化,每个功能拆分为独立组件。例如产品列表、详情页、购物车等可分别封装为ProductList.vue、Pro…

php实现打印功能

php实现打印功能

PHP 实现打印功能的方法 PHP 可以通过多种方式实现打印功能,具体取决于打印的目标(如打印机、PDF、网页等)和需求(如直接打印、生成打印预览等)。以下是几种常见的实现方式: 使用 JavaSc…

vue如何实现

vue如何实现

Vue 实现方法 Vue 提供了多种方式来实现功能,具体取决于需求。以下是一些常见场景的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。在组件中可以通过 pro…

php如何实现直播

php如何实现直播

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