I’m uisng FPDF for my PDF handling in my webshop. Whenever a user checks out I want to generate a PDF with all the data from the shopping cart and afterwards upload the PDF to the media library in WordPress.
I’ve tried a bunch of stuff now, but I haven’t made anything yet that works. So far I can only save the PDF to a different folder by writing:
$pdf->Output($_SERVER['DOCUMENT_ROOT'].'/invoice/nameofinvoice.pdf', 'F');
My code looks like this:
if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $pdf->Output('dimserPdf2014.pdf', 'F');
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
// die(var_dump($movefile));
$wp_filetype = $movefile['type'];
$filename = $movefile['file'];
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $wp_filetype,
'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
// Id of attachment if needed
$attach_id = wp_insert_attachment( $attachment, $filename);
It does upload a file to the media library, but the file is empty, and if I uncomment the die > var_dump function I also get an array with simply nothing saying it’s an empty file.
This is the script/class I’m using:
http://www.fpdf.org/
and the Output method accepts two parameters, filename and method of saving/downloading. As you can see here:
http://www.fpdf.org/en/doc/output.htm and I’ve tried every single parameter so far with no luck.
Any idea on why WordPress uploads an empty file and not my PDF?
I’ve been doing some work with mPDF, which is a (better documented & maintained imho) fork of FPDF. From what I can see in the documentation, the Output method doesn’t return anything unless you pass the “S” dest.
Also, according to the Codex the file in
wp_handle_upload
is for working on one item in the$_FILES
superglobal. You shouldn’t need it in your case if you generate your PDF in the uploads directory.Try this instead: