refactor(pdf): simplify PDF module by removing redundant utilities
Remove helper functions (cmToPoints, inchesToPoints, mmToPoints, createElement, PAGE_SIZES) and consolidate re-exports from @react-pdf/renderer into a single export statement. Retain only the getFilename utility and streamline the module to reduce unnecessary abstraction over the underlying library.
This commit is contained in:
+4
-116
@@ -1,121 +1,9 @@
|
|||||||
/**
|
export { renderToBuffer, Document, Page, View, Text, Image, Link, StyleSheet, Font } from '@react-pdf/renderer';
|
||||||
* PDF Generation Utilities
|
|
||||||
* Wrapper around @react-pdf/renderer for PDF generation
|
|
||||||
*
|
|
||||||
* Usage in modules:
|
|
||||||
* import { renderToBuffer } from '@zen/core/pdf';
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { renderToBuffer as reactPdfRenderToBuffer } from '@react-pdf/renderer';
|
|
||||||
import React from 'react';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a React PDF document to a buffer
|
* Get a suggested filename for a PDF.
|
||||||
* @param {React.Element} document - React PDF document element
|
* @example getFilename('invoice', '12345') // 'invoice-12345-2024-01-15.pdf'
|
||||||
* @returns {Promise<Buffer>} PDF buffer
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* import { Document, Page, Text } from '@react-pdf/renderer';
|
|
||||||
*
|
|
||||||
* const MyDoc = () => (
|
|
||||||
* <Document>
|
|
||||||
* <Page>
|
|
||||||
* <Text>Hello World</Text>
|
|
||||||
* </Page>
|
|
||||||
* </Document>
|
|
||||||
* );
|
|
||||||
*
|
|
||||||
* const buffer = await renderToBuffer(<MyDoc />);
|
|
||||||
*/
|
|
||||||
export async function renderToBuffer(document) {
|
|
||||||
return await reactPdfRenderToBuffer(document);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a React element for PDF rendering
|
|
||||||
* @param {Function} Component - React component
|
|
||||||
* @param {Object} props - Component props
|
|
||||||
* @returns {React.Element}
|
|
||||||
*/
|
|
||||||
export function createElement(Component, props) {
|
|
||||||
return React.createElement(Component, props);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a suggested filename for a PDF
|
|
||||||
* @param {string} prefix - Filename prefix
|
|
||||||
* @param {string|number} identifier - Unique identifier
|
|
||||||
* @param {Date} date - Date for the filename (default: today)
|
|
||||||
* @returns {string} Suggested filename
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* getFilename('invoice', '12345'); // 'invoice-12345-2024-01-15.pdf'
|
|
||||||
*/
|
*/
|
||||||
export function getFilename(prefix, identifier, date = new Date()) {
|
export function getFilename(prefix, identifier, date = new Date()) {
|
||||||
const dateStr = date.toISOString().split('T')[0];
|
return `${prefix}-${identifier}-${date.toISOString().split('T')[0]}.pdf`;
|
||||||
return `${prefix}-${identifier}-${dateStr}.pdf`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert centimeters to points (for PDF dimensions)
|
|
||||||
* @param {number} cm - Centimeters
|
|
||||||
* @returns {number} Points
|
|
||||||
*/
|
|
||||||
export function cmToPoints(cm) {
|
|
||||||
return cm * 28.3465;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert inches to points (for PDF dimensions)
|
|
||||||
* @param {number} inches - Inches
|
|
||||||
* @returns {number} Points
|
|
||||||
*/
|
|
||||||
export function inchesToPoints(inches) {
|
|
||||||
return inches * 72;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert millimeters to points (for PDF dimensions)
|
|
||||||
* @param {number} mm - Millimeters
|
|
||||||
* @returns {number} Points
|
|
||||||
*/
|
|
||||||
export function mmToPoints(mm) {
|
|
||||||
return mm * 2.83465;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Common page sizes in points
|
|
||||||
*/
|
|
||||||
export const PAGE_SIZES = {
|
|
||||||
A4: { width: 595.28, height: 841.89 },
|
|
||||||
LETTER: { width: 612, height: 792 },
|
|
||||||
LEGAL: { width: 612, height: 1008 },
|
|
||||||
A3: { width: 841.89, height: 1190.55 },
|
|
||||||
A5: { width: 419.53, height: 595.28 },
|
|
||||||
};
|
|
||||||
|
|
||||||
// Re-export react-pdf components for convenience
|
|
||||||
export {
|
|
||||||
Document,
|
|
||||||
Page,
|
|
||||||
View,
|
|
||||||
Text,
|
|
||||||
Image,
|
|
||||||
Link,
|
|
||||||
StyleSheet,
|
|
||||||
Font,
|
|
||||||
PDFViewer,
|
|
||||||
BlobProvider,
|
|
||||||
PDFDownloadLink,
|
|
||||||
} from '@react-pdf/renderer';
|
|
||||||
|
|
||||||
// Default export
|
|
||||||
export default {
|
|
||||||
renderToBuffer,
|
|
||||||
createElement,
|
|
||||||
getFilename,
|
|
||||||
cmToPoints,
|
|
||||||
inchesToPoints,
|
|
||||||
mmToPoints,
|
|
||||||
PAGE_SIZES,
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user