PHP:mPDF/pdf库/是否可以在一个循环中创建多个pdf文件? [英] PHP: mPDF / pdf library / is it possible to create multiple pdf files in a loop?

查看:236
本文介绍了PHP:mPDF/pdf库/是否可以在一个循环中创建多个pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mPDF库,并且正在尝试编写一个代码,该代码将在一次调用中创建多个PDF文件(其中一个打开项为test.php).问题:总是只下载第一个文件.我知道这个问题似乎很愚蠢,我已经尝试过深入研究Google,但我不知道该如何解决.也许有人可以带领我走上正确的道路?

我的逻辑是:
-我有一个包含所有订单的阵列;我想为每个订单创建PDF.
-我循环数组,每次调用MPDF库

I'm using mPDF library and I'm trying to write a code which will create multiple PDF files at one call (with one opening of: test.php). Problem: always only the first file is downloaded. I know that the question may seems stupid, I've tried to dive deep into google but I can't figure out how to solve this. Maybe someone could lead me on the right path?

My logic is:
- I have one array with all of orders; for each order I would like to create PDF.
- I loop the array and each time call the MPDF library

test.php

$myorders = [ 
                0 => array (
                'FirstName' => 'Asia',
                'LastName' => 'Basia',
                'OrderNr' => '123'

                ), 

                1 => array (
                'FirstName' => 'Madzia',
                'LastName' => 'Siasia',
                'OrderNr' => '456'
                )
           ];

for ($i=0; $i < count($myorders); $i++) { 
    $FirstName = $myorders[$i]['FirstName'];
    $LastName = $myorders[$i]['LastName'];
    $OrderNr = $myorders[$i]['OrderNr'];
    require 'invoice.php';
}

invoice.php

require 'MPDF57/mpdf.php';
$mpdf=new mPDF();
$mpdf->Bookmark('Start of the document');
$mpdf->WriteHTML('test test' . $FirstName);
$mpdf->Output("filename{$OrderNr}.pdf",'D');

仅下载filename123.pdf.不能下载多个文件吗?我究竟做错了什么?

Only filename123.pdf is downloaded. Is it not possible to download more than one file? What am I doing wrong?

推荐答案

更新后的问题的答案是:

The answer for your updated question is:

当然,您可以创建多个pdf文件并将其保存到光盘上.您只需要将pdf生成代码放在for循环中即可.

Ofcourse you can create multiple pdf files and save them onto disc. You just have to place the pdf generation code inside your for loop.

请参考以下代码:

require 'MPDF57/mpdf.php';

for ($i=0; $i < count($myorders); $i++) { 
    $FirstName = $myorders[$i]['FirstName'];
    $OrderNr = $myorders[$i]['OrderNr'];
    $mpdf=new mPDF();
    $mpdf->Bookmark('Start of the document');
    $mpdf->WriteHTML('test test' . $FirstName);
    $mpdf->Output("filename{$OrderNr}.pdf", 'F');
}

这篇关于PHP:mPDF/pdf库/是否可以在一个循环中创建多个pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆