首页 > 解决方案 > 自动从 href 下载生成的 pdf

问题描述

我真的很沮丧,我没有解决这个问题。

我在php.

我想要的只是生成pdf的,单击生成后自动下载的链接时可用。

正如你所看到的,我已经尝试了一些东西,但我不明白。

总结一下

我希望pdf单击此链接后显示的文件<a href="'.get_home_url().'/?dpd_id='.$label_number.'" download>Click here to download PDF</a>无需单击即可自动下载。

谁能帮我吗 ?

// 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' );
?>

标签: phppdfdownload

解决方案


首先,您需要Content-Disposition: Attachment在生成的 PDF 文件上设置 HTTP 标头。为此,您需要修改 PDF 生成代码。否则,下面的方法只会导致 PDF 显示(不会自动下载)。

完成后,链接到 HTML<head>标记中生成的 PDF 文件,如下所示:

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

如果 PDF 的路径是相对的,请在url=字段中的 URL 前加上./.

确保以label_number 某种方式验证参数,否则您的应用程序将容易受到未经验证的重定向安全漏洞的攻击。我还建议在页面某处保留指向 PDF 文件的链接,以防由于某种原因无法自动开始下载。

另请参阅如何强制自动下载 pdf?元刷新下载(txt、ini、css 或 html)文件


推荐阅读