UNPKG

react-pdf-wrapper

Version:

A lightweight and flexible React wrapper for exporting fully customized, multi-page PDFs directly from your React components. Say goodbye to generic, static PDF layouts — simply wrap your content with our component to deliver elegant, branded documents t

50 lines (47 loc) 1.3 kB
import React, { forwardRef, useImperativeHandle } from 'react'; import html2pdf from 'html2pdf.js'; const PdfWrapper = /*#__PURE__*/forwardRef(({ children, pageRefs, pdfName }, ref) => { const handleDownloadPDF = async () => { if (pageRefs.some(ref => !ref.current)) return; try { const pdfOptions = { margin: 0, filename: `${pdfName}.pdf`, image: { type: "jpeg", quality: 0.98 }, html2canvas: { scale: 2, useCORS: true }, jsPDF: { orientation: "portrait", unit: "mm", format: [250, 410] } }; const container = document.createElement("div"); pageRefs.forEach((ref, index) => { const pageClone = ref.current.cloneNode(true); if (index < pageRefs.length - 1) { pageClone.style.pageBreakAfter = "always"; } container.appendChild(pageClone); }); await html2pdf().from(container).set(pdfOptions).save(); } catch (err) { console.error(err); } }; useImperativeHandle(ref, () => ({ handleDownloadPDF })); return /*#__PURE__*/React.createElement(React.Fragment, null, children); }); export { PdfWrapper as default }; //# sourceMappingURL=index.js.map