dompdf生成多个pdf保存到文件夹,然后作为附件发送给phpmailer

时间:2018-10-16 06:36:56

标签: dompdf

我有3部分代码,如下所示。

    <?php
include_once '.././connect/init.php';
global $db;
if (isset($_GET['success']) && empty($_GET['success'])) {
    $registerd_succ = '<p class="success2" style="width: 100%">' . ' Mail with invoice sent successfully.' . '</p>';
    echo $registerd_succ;
}
if (isset($_GET['failure']) && empty($_GET['failure'])) {
    $registerd_succ = '<p class="success2" style="width: 100%">' . 'Failed to send Mail with invoice.' . '</p>';
    echo $registerd_succ;
}

//include autoloader
require_once '.././dompdf/dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;

//initialize dompdf class
$options = new Options();
$options->setIsRemoteEnabled(true);

$document = new Dompdf($options);

$contxt = stream_context_create([
    'ssl' => [
        'verify_peer'      => FALSE,
        'verify_peer_name' => FALSE,
        'allow_self_signed'=> TRUE
    ]
]);
$document->setHttpContext($contxt);

// section 1 code Generate PDF for Invoice
$output = 'My invoice HTML';
$document->loadHtml($output);
$document->setPaper('A4', 'potrait');
$document->render();
$invhead  = 'Invoice-No-' . $inv_no . '.pdf';
$invhead1 = 'Invoice-No-' . $inv_no;

$dir_to_save = "../invoices/";

// Save file to server folder
$output1 = $document->output(['isRemoteEnabled' => true]);
file_put_contents($dir_to_save . $invhead, $output1);

//  $document->stream("$invhead1", array("Attachment" => 0));
//1  = Download
//0 = Preview

// Section 2 code Generate PDF for packing list
$document_pl = new Dompdf();
$output_pl = 'Packing list content';
    $document_pl->loadHtml($output_pl);
    $document_pl->setPaper('A4', 'potrait');
    $document_pl->render();
    $packlist  = 'Packing List-No-' . $id . '.pdf';
    $packlist1 = 'Packing List-No-' . $id;

    $dir_to_save = "../invoices/";

// Save file to server folder
    $output2 = $document_pl->output();
    file_put_contents($dir_to_save . $packlist, $output2);

    // $document_pl->stream("$packlist1", array("Attachment" => 0));
    //1  = Download
    //0 = Preview

// Section 3 code - Sending mail with attachment of PDF files with PhpMailer

// SEND EMAIL WITH ATTACHMENTS

$email   = 'kpkdhar22@gmail.com';
$name    = 'Poorna Kaladhar';
$phone   = '9951554741';
$message = 'Sample Message';

require("./PHPMailer/src/PHPMailer.php");
require("./PHPMailer/src/SMTP.php");
require("./PHPMailer/src/Exception.php");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer();

$mail->IsSMTP();

$mail->Host = "smtp.gmail.com";

$mail->SMTPAuth = true;

$file_name      = $invhead;
$file_name1     = $packlist;
$mail->Username = "kpkdhar22@gmail.com"; // SMTP username
$mail->Password = "XXXXXXXX"; // SMTP password
$mail->addAttachment("invoices/" . $invhead);
//  $mail->addAttachment("../invoices/" . $file_name1);
$mail->From       = "kpkdhar22@gmail.com";
$mail->FromName   = "XXXXXX";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465; //SMTP port
$mail->addAddress("kpkdhar22@gmail.com", "Poorna xxxxxx");
$mail->Subject = "Your invoice from xxxxxxxxx";
$mail->Body    = "
                    Name: $name<br>
                    Email: $email<br>
                    Telephone: $phone<br><br><br>
                    Comments: $message";
$mail->AltBody = $message;

if (!$mail->Send()) {
    header('location:send_inv_packlist1.php?success');
    exit();
} else {
    header('location:send_inv_packlist1.php?failure');
    exit();
}

当我用1,2,3运行代码时,仅运行第1部分代码,然后pdf在浏览器中显示。为什么第二个pdf没有生成,在调试时我的光标运行到$ document_pl-> render();然后退出。 当我用1,3或2,3运行代码时,仍生成的pdf显示在浏览器中,并且邮件带有附件发送。但是它不会重定向到成功或失败消息。只是pdf显示在浏览器中。 我希望生成两个PDF并将其保存在目录中,并将它们作为附件发送到邮件。我可以使用现有文件发送多个附件,但在上述情况下不可以。

任何建议或有用的指导都将受到赞赏。

0 个答案:

没有答案