1

I'm really down i don't get a solution for this.

I have the following code in php.

All i want is that the generated pdf which is available when clicking on the link to be downloaded automatically after it is generated.

As you can see i have tried a few things but i dont get it.

to summarize :

I want the pdf file which is showing after clicking on this link <a href="'.get_home_url().'/?dpd_id='.$label_number.'" download>Click here to download PDF</a> to be downloaded automatically without the need to click it.

Can anyone help me out ?

// Make the action from selected orders
add_filter( 'handle_bulk_actions-edit-shop_order', 'dpd_handle_bulk_generate_labels', 10, 3 );
function dpd_handle_bulk_generate_labels( $redirect_to, $action, $post_ids ) {
    if ( $action !== 'dpd_bulk_create_label' )
        return $redirect_to; // Exit

    global $attach_download_dir, $attach_download_file; // ???

    $success_ids = array();
    $failed_ids = array();

    $messages = array();
    echo "<br>Post ID: ";
    echo "<PRE>";
    print_r($post_ids);
    echo "</PRE>";

    foreach ( $post_ids as $post_id ) {

        $response_error = create_label($post_id, 'return');
        if(!$response_error)
            {
                $success_ids[] = $post_id;  
            }
        else
            {
                $failed_ids[] = $post_id;   
            }

        $order = wc_get_order( $post_id );
        $order_data = $order->get_data();

        // Your code to be executed on each selected order
        fwrite($myfile,
            $order_data['date_created']->date('d/M/Y') . '; ' .
            '#' . ( ( $order->get_type() === 'shop_order' ) ? $order->get_id() : $order->get_parent_id() ) . '; ' .
            '#' . $order->get_id()
        );
        $processed_ids[] = $post_id;
        $messages[] = "Generated Shipping Label for Order ID: ".$post_id;
    }
    $msg = implode("<br>", $messages);
    echo "<br>MSG: ".$msg;
    new DPD_notify( $msg, "notice notice-success" );
//exit;
    return $redirect_to;
    return $redirect_to = add_query_arg( array(
        'write_downloads' => '1',
        'sids' => implode( ',', $success_ids ),
        'fids' => implode( ',', $failed_ids ),
    ), $redirect_to );
}
$ch = curl_init();
$source = "'.get_home_url().'/?dpd_id='.$label_number.'";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$destination = "'download.pdf";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

function dpd_label_on_order_status_completed( $order_id ) 
    { 
        $auto_generate_shipping_label = ice_get_option( 'dpd_auto_generate_shipping_label_oc', 'dpd_options' );
        if($auto_generate_shipping_label)   create_label($order_id, 'return');
    }
add_action( 'woocommerce_order_status_completed', 'dpd_label_on_order_status_completed', 10, 1 ); 

function get_order_shipping_label_number($order_id)
    {
        $label_number = 0;
        if($order_id)
            {
                global $wpdb;
                $table_name = $wpdb->prefix.'dpd_orders';
                $parcels = $wpdb->get_results("SELECT id, parcel_number, date FROM $table_name WHERE order_id = $order_id AND (order_type != 'amazon_prime' OR order_type IS NULL ) AND status !='trash'");
                if( count ( $parcels ) > 0 ) {
                    if(isset($parcels[0]->id)) {
                        $label_number = $parcels[0]->id;    
                    }
                }   
            }
        return $label_number;
    }

// The results notice from bulk action on orders
add_action( 'admin_notices', 'downloads_bulk_action_admin_notice' );
function downloads_bulk_action_admin_notice() {
    $success_ids = '';  if(isset($_REQUEST['sids']))    $success_ids = $_REQUEST['sids'];
    $failed_ids = '';   if(isset($_REQUEST['fids']))    $failed_ids = $_REQUEST['fids'];

    if($success_ids)
        {
            echo '<div class="notice notice-success"><br>';
            echo "Successfully generated label for the following Orders: ";
            $success_ids_array = explode(",", $success_ids);
            if(is_array($success_ids_array) and sizeof($success_ids_array))
                {
                    foreach($success_ids_array as $success_id)
                        {
                            $label_number = get_order_shipping_label_number($success_id);
                            echo '<br>Order ID: '.$success_id.' - <a href="'.get_home_url().'/?dpd_id='.$label_number.'" download>Click here to download PDF</a>;

                        }
                }
    if($failed_ids)
        {
            echo "<br>Failed generating Labels for following Order numbers: ".$failed_ids;
        }
}

function dpd_wc_add_label_pdf_column_header( $columns ) {
    $new_columns = array();
    foreach ( $columns as $column_name => $column_info ) {
        $new_columns[ $column_name ] = $column_info;
        if ( 'order_total' === $column_name ) {
            $new_columns['shipping_label'] = __( 'Shipping Label', 'my-textdomain' );
        }
    }
    return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'dpd_wc_add_label_pdf_column_header', 20 );

function dpd_wc_add_label_pdf_column_content( $column ) {
    global $post;
    if ( 'shipping_label' === $column ) {
        $label_number = get_order_shipping_label_number($post->ID);
        if($label_number)   echo '<a href="'.get_home_url().'/?dpd_id='.$label_number.'" target="_blank">Download PDF</a>'.trigger("click");
file_put_contents("download.pdf", fopen("'.get_home_url().'/?dpd_id='.$label_number.'", 'r'));
    }
}
add_action( 'manage_shop_order_posts_custom_column', 'dpd_wc_add_label_pdf_column_content' );
?>
Fmerco
  • 1,160
  • 5
  • 20

1 Answers1

1

First, you need to set the Content-Disposition: Attachment HTTP header on the generated PDF file. You will need to modify the PDF generation code to do this. Otherwise, the method below will only cause the PDF to display (it won't be downloaded automatically).

Once this is done, link to the generated PDF file in the HTML <head> tag like this:

<meta http-equiv="refresh" content="0; url=<?= get_home_url().'/?dpd_id='.$label_number ?> />

If the path to the PDF is relative, prefix the URL in the url= field with ./.

Make sure to validate the label_number parameter somehow, or your application will be vulnerable to an unvalidated redirects security vulnerability. I would also recommend keeping a link to the PDF file somewhere in the page, in case the download doesn't start automatically for some reason.

See also How to force a pdf download automatically? and Meta refresh download (txt, ini, css or html) file.

Calinou
  • 889
  • 8
  • 14
  • With your anser is was able to make the pdf download when the link is clicked automatcally. Thank you. But it doesn´t download it without click. i added your code to header but nothing happens – user12961845 Feb 26 '20 at 16:50