Hello
The PDFs are not stored in the database or on the server, they are generated in real-time.
You can create this feature though by registering a new post type that has an archive, and then creating a post and saving the PDF to the filesystem and storing it as post meta. Below is an example, assuming you have a registered post type with the slug of pdf
/**
* Save the PDF
*
* @param \Mpdf\Mpdf $mpdf Mpdf object.
* @return void
*/
function wc_cart_pdf_save_pdf_to_database( $mpdf ) {
// Get the WordPress upload directory
$upload_dir = wp_upload_dir();
$upload_path = $upload_dir['basedir'] . '/wc-cart-pdf/';
// Ensure the directory exists
if ( ! file_exists( $upload_path ) ) {
wp_mkdir_p( $upload_path );
}
// Construct the file path
$file_name = apply_filters( 'wc_cart_pdf_filename', 'WC_Cart-' . gmdate( 'Ymd' ) . bin2hex( openssl_random_pseudo_bytes( 5 ) ) ) . '.pdf';
$file_path = $upload_path . $file_name;
// Output the PDF to the specified path
$mpdf->Output( $file_path, 'F' );
// Insert a new post of type 'pdf'
$post_data = array(
'post_title' => wp_strip_all_tags( $file_name ), // Use the file name as the post title
'post_content' => '', // You can add content if needed
'post_status' => 'publish', // Or 'draft' if you don't want it to be published immediately
'post_type' => 'pdf', // You can use any post type you want
);
// Insert the post into the database
$post_id = wp_insert_post( $post_data );
// Optionally, store the file path or URL in post meta if needed
if ( ! is_wp_error( $post_id ) ) {
$file_url = $upload_dir['baseurl'] . '/wc-cart-pdf/' . $file_name;
update_post_meta( $post_id, '_pdf_file_url', $file_url ); // Save the URL of the PDF in post meta
}
}
add_action( 'wc_cart_pdf_output', 'wc_cart_pdf_save_pdf_to_database' );