【问题标题】:how can I download multiple PDF files using php如何使用 php 下载多个 PDF 文件
【发布时间】:2020-02-24 14:48:13
【问题描述】:

所以我正在制作一个网站,允许用户从 xlsx 文件表中读取并下载所有数据,每个数据都以单独的 pdf 格式下载,这是代码

    <?php

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

// Load Composer's autoloader
require 'vendor/autoload.php';
$file_name="";
//index.php
$message = '';
require_once __DIR__.'/../src/SimpleXLSX.php';
echo '<h1>XLSX to HTML</h1>';

if (isset($_FILES['file'])) {
    if ( $xlsx = SimpleXLSX::parse( $_FILES['file']['tmp_name'] ) ) {
        $filen=$_FILES['file']['tmp_name'];
        echo '<h2>'.$xlsx->sheetName($_POST['sh']-1).'</h2>';

        echo '<table border=1>';
        $dim = $xlsx->dimension();
        $num_cols = $dim[0];
        $num_rows = $dim[1];
        foreach ( $xlsx->rows($_POST['sh']-1) as $k => $r ) {
            //  if ($k == 0) continue; // skip first row
            echo '<tr>';
            if ($k == 0) echo '<td>' .$r[ 0 ]. '</td>';
else
            echo '<td>' .substr_replace($r[ 0 ],"",strripos($r[ 0 ]," ")). '</td>';
            echo '<td>' .$r[ 1 ]. '</td>';
            echo '<td>' .$r[ 2 ]. '</td>';
            echo '<td>' .$r[ 4 ]. '</td>';
            echo'<td>' . $r[ 5 ]. '</td>';
            echo'<td>' . $r[ 7 ]. '</td>';
            echo'<td>' .$r[ 8 ] . '</td>';
            echo '</tr>';
                if ($k != 0) // skip first row

            {$date = substr_replace($r[0], "", strripos($r[0], " "));
            $factname = $r[1];
            $name = $r[2];

            $email = $r[4];
            $phone = $r[5];
            $post = $r[7];
            $pack = $r[8];
                echo $name;

                if ($pack == '90') $garanti = '30 jours';
                else if ($pack == '190') $garanti = '6 mois';
                else if ($pack == '290') $garanti = '12 mois';
                else if ($pack == '390') $garanti = '2 ans';
                else if ($pack == '490') $garanti = '3 ans';
                else if ($pack == '590') $garanti = '5 ans';

                sendmail();
                echo'<td>telecharger</td>';}

              //  echo "telecharger";

            }
            echo '</table>';
            echo '</tr>';

        }

    echo '</table>';
}

else {
    echo SimpleXLSX::parseError();
}

if(isset($_POST['charge'])) {

    if (isset($_FILES['file'])) {
        if ($xlsx = SimpleXLSX::parse($_FILES['file']['tmp_name'])) {


            foreach ($xlsx->rows($_POST['sh']-1) as $k => $r) {
                if ($k == 0) continue; // skip first row

                $date = substr_replace($r[0], "", strripos($r[0], " "));
                $factname = $r[1];
                $name = $r[2];
                $email = $r[4];
                $phone = $r[5];
                $post = $r[7];
                $pack = $r[8];
                if ($pack == '90') $garanti = '30 jours';
                else if ($pack == '190') $garanti = '6 mois';
                else if ($pack == '290') $garanti = '12 mois';
                else if ($pack == '390') $garanti = '2 ans';
                else if ($pack == '490') $garanti = '3 ans';
                else if ($pack == '590') $garanti = '5 ans';

                sendmail();
                echo "telecharger";
            }


        }

        echo "telecharger";
    }
}

echo '<h2>Upload form</h2>

<form method="post" enctype="multipart/form-data">
*.XLSX <input type="file" name="file"  />
<input placeholder="sheet number" name="sh" type="number" required>
&nbsp;&nbsp;<input type="submit" value="Parse" />
</form>';


function sendmail()
{
    global $name;
    global $file_name;
    $file_name="";

    echo $file_name;
    include('pdf.php');
    $pdf = new Pdf();
    $file_name = "ORDER-".$name . '.pdf';

    $html_code = '<link rel="stylesheet" href="bootstrap.min.css">';
    $html_code .= fetch_customer_data();

    $pdf->load_html($html_code);
    $pdf->render();
    $file = $pdf->output();
    file_put_contents($file_name, $file);
    // $pdf->stream($file_name) ->

}

这是 pdf.php 文件

<?php

//pdf.php

require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

class Pdf extends Dompdf{

 public function __construct(){
  parent::__construct();
 }
}

?>

我想同时下载所有 pdf,但它只下载第一个并显示此错误

( ! ) Fatal error: Cannot declare class Pdf, because the name is already in use in C:\wamp64\www\vucrm\xl\simplexlsx-master\examples\pdf.php on line 0

我尝试在 sendmail 函数的末尾添加exit(),但这只会下载第一个并且没有显示其他数据或错误 谁能帮忙提前谢谢

【问题讨论】:

  • 是你自己的课,还是供应商的课?
  • 这是供应商的

标签: php pdf


【解决方案1】:

您需要在脚本顶部使用require_once,不要在函数内部使用include

// Require this at the top of your file
require_once('pdf.php');

问题是每次调用该函数时,它都包含PDF类再次,并且只能声明一次。

【讨论】:

  • 这修复了错误,但我想使用 $pdf->stream($file_name) 将它们下载到下载文件夹中;但是我被 IDM 屏蔽了你知道为什么吗
  • 我认为你不能 (stackoverflow.com/questions/43037622/…)。但是,您可以将它们写入 zip 文件并下载单个存档。
  • 不能自动下载吗?
  • 谢谢,但我认为这无济于事,我的客户希望将它们自动下载到他的计算机中
【解决方案2】:

可在 html 链接中下载 PDF 文件!

借助 php 中的 header() 函数,使用 PHP 从 HTML 链接下载 PDF。

header() 函数用于发送原始 HTTP 标头。 有时它希望提示用户保存生成的 PDF 等数据。

语法

下载任何应用程序的http响应头

header("Content-Type: application/octet-stream");

http 响应头设置组合和文件下载

header('Content-Disposition: attachment; filename="downloaded.pdf"');

需要下载的请求文件长度

header("Content-Length: " . filesize("download.pdf"));

读取文件并将其写入输出缓冲区。

readfile('original.pdf');

PHP 代码

$file = $_GET["file"] .".pdf";  
// We will be outputting a PDF 

header('Content-Type: application/pdf');   
// It will be called downloaded.pdf 

header('Content-Disposition: attachment; filename="gfgpdf.pdf"');   
$imagpdf = file_put_contents($image, file_get_contents($file));    
echo $imagepdf; 

HTML 代码

<!DOCTYPE html> 
<html> 

<head> 
    <title>Download PDF using PHP from HTML Link</title> 
</head> 

<body> 
    <center> 
        <h2 style="color:green;">Welcome To GFG</h2> 
        <p><b>Click below to download PDF</b> 
        </p> 
        <a href="gfgpdf.php?file=gfgpdf">Download PDF Now</a></center> 
</body> 

</html>

注意:请记住,必须在发送任何实际输出之前调用 HTTP header(),无论是通过普通 HTML 标记、文件中的空白行还是从 PHP 发送。

示例1:将下面的HTML文件另存为htmllinkpdf.html,将PHP文件另存为downloadpdf.php

以上示例说明了使用 HTML 链接下载 PDF 文件的概念。 下载的文件似乎是 PDF 格式,但没有任何内容在任何应用程序中打开时显示错误。

more here

这是另一个simple solution in for loop

【讨论】:

    猜你喜欢
    • 2015-07-05
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多