react-easy-export
Version:
A React library for easily exporting data to CSV, PDF, and Excel formats.
26 lines (22 loc) • 830 B
JavaScript
/**
* Exports data as a PDF file using the browser's print functionality.
* @param {string} content - The HTML content to be printed to PDF.
* @param {string} [filename='data.pdf'] - Suggested name of the exported file (though this can't be enforced through the print dialog).
*/
const exportToPDF = (content, filename = 'data.pdf') => {
const printWindow = window.open('', '_blank');
if (!printWindow) {
alert('Please allow popups to print content.');
return;
}
printWindow.document.write(content);
printWindow.document.title = filename;
printWindow.document.close();
printWindow.onload = function() {
printWindow.print();
printWindow.onafterprint = function() {
printWindow.close();
};
};
};
export default exportToPDF;