当前位置: 首页 > 知识库问答 >
问题:

Prestashop 1.6-创建pdf(tcpdf)并将其作为电子邮件附件发送

叶裕
2023-03-14

使用Prestashop 1.6

我有以下代码来生成PDF

<?php
//============================================================+
// File name   : example_001.php
// Begin       : 2008-03-04
// Last Update : 2013-05-14
//
// Description : Example 001 for TCPDF class
//               Default Header and Footer
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com LTD
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: Default Header and Footer
 * @author Nicola Asuni
 * @since 2008-03-04
 */

 require_once(dirname(__FILE__).'/../config/config.inc.php');
 require_once(dirname(__FILE__).'/../init.php');


// Include the main TCPDF library (search for installation path).
// require_once('tcpdf_include.php');
require_once(dirname(__FILE__).'/../tools/tcpdf/config/tcpdf_config.php');
require_once(dirname(__FILE__).'/../tools/tcpdf/tcpdf.php');


// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 027');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
// $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 027', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set a barcode on the page footer
$pdf->setBarcode(date('Y-m-d H:i:s'));

// set font
$pdf->SetFont('helvetica', '', 11);

// add a page
$pdf->AddPage();

// print a message
$txt = "You can also export 1D barcodes in other formats (PNG, SVG, HTML). Check the examples inside the barcodes directory.\n";
$pdf->MultiCell(70, 50, $txt, 0, 'J', false, 1, 125, 30, true, 0, false, true, 0, 'T', false);
$pdf->SetY(30);

// -----------------------------------------------------------------------------

$pdf->SetFont('helvetica', '', 10);

// define barcode style
$style = array(
    'position' => '',
    'align' => 'C',
    'stretch' => false,
    'fitwidth' => true,
    'cellfitalign' => '',
    'border' => true,
    'hpadding' => 'auto',
    'vpadding' => 'auto',
    'fgcolor' => array(0,0,0),
    'bgcolor' => false, //array(255,255,255),
    'text' => true,
    'font' => 'helvetica',
    'fontsize' => 8,
    'stretchtext' => 4
);

// PRINT VARIOUS 1D BARCODES

// CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
$pdf->Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
$pdf->write1DBarcode('CODE 39', 'C39', '', '', '', 18, 0.4, $style, 'N');

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('example_027.pdf', 'I');

我有以下代码发送电子邮件:

require_once(dirname(__FILE__).'/../config/config.inc.php');
require_once(dirname(__FILE__).'/../init.php');

include_once(_PS_SWIFT_DIR_.'Swift.php');
include_once(_PS_SWIFT_DIR_.'Swift/Connection/SMTP.php');
include_once(_PS_SWIFT_DIR_.'Swift/Connection/NativeMail.php');
include_once(_PS_SWIFT_DIR_.'Swift/Plugin/Decorator.php');

$id_shop = 1;

if (!$id_shop) {
    $id_shop = Context::getContext()->shop->id;
}

$configuration = Configuration::getMultiple(array(
    'PS_SHOP_EMAIL',
    'PS_MAIL_METHOD',
    'PS_MAIL_SERVER',
    'PS_MAIL_USER',
    'PS_MAIL_PASSWD',
    'PS_SHOP_NAME',
    'PS_MAIL_SMTP_ENCRYPTION',
    'PS_MAIL_SMTP_PORT',
    'PS_MAIL_TYPE'
), null, null, $id_shop);

$from = $configuration['PS_SHOP_EMAIL'];
$from_name = '';
$connection = new Swift_Connection_SMTP(
    $configuration['PS_MAIL_SERVER'],
    $configuration['PS_MAIL_SMTP_PORT'],
    $configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl' ? Swift_Connection_SMTP::ENC_SSL : (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls' ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF))
);
$connection->setTimeout(4);

if (!$connection) {
    echo false;die;
}
if (!empty($configuration['PS_MAIL_USER'])) {
    $connection->setUsername($configuration['PS_MAIL_USER']);
}
if (!empty($configuration['PS_MAIL_PASSWD'])) {
    $connection->setPassword($configuration['PS_MAIL_PASSWD']);
}

$swift = new Swift($connection, Configuration::get('PS_MAIL_DOMAIN', null, null, $id_shop));

$subject = '['.Configuration::get('PS_SHOP_NAME', null, null, $id_shop).'] Tracking barcode';
$message = new Swift_Message($subject);

$message->setCharset('utf-8');

$message->headers->setEncoding('Q');

$to_list = new Swift_RecipientList();

$to_list->addTo('tst@gmail.com', 'tst');
$send = $swift->send($message, $to_list, new Swift_Address($from, $from_name));
$swift->disconnect();

现在,我尝试创建一个脚本,生成PDF并将其作为电子邮件附件发送:

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: Default Header and Footer
 * @author Nicola Asuni
 * @since 2008-03-04
 */

 require_once(dirname(__FILE__).'/../config/config.inc.php');
 require_once(dirname(__FILE__).'/../init.php');


// Include the main TCPDF library (search for installation path).
// require_once('tcpdf_include.php');
require_once(dirname(__FILE__).'/../tools/tcpdf/config/tcpdf_config.php');
require_once(dirname(__FILE__).'/../tools/tcpdf/tcpdf.php');


// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 027');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
// $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 027', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set a barcode on the page footer
$pdf->setBarcode(date('Y-m-d H:i:s'));

// set font
$pdf->SetFont('helvetica', '', 11);

// add a page
$pdf->AddPage();

// print a message
$txt = "You can also export 1D barcodes in other formats (PNG, SVG, HTML). Check the examples inside the barcodes directory.\n";
$pdf->MultiCell(70, 50, $txt, 0, 'J', false, 1, 125, 30, true, 0, false, true, 0, 'T', false);
$pdf->SetY(30);

// -----------------------------------------------------------------------------

$pdf->SetFont('helvetica', '', 10);

// define barcode style
$style = array(
    'position' => '',
    'align' => 'C',
    'stretch' => false,
    'fitwidth' => true,
    'cellfitalign' => '',
    'border' => true,
    'hpadding' => 'auto',
    'vpadding' => 'auto',
    'fgcolor' => array(0,0,0),
    'bgcolor' => false, //array(255,255,255),
    'text' => true,
    'font' => 'helvetica',
    'fontsize' => 8,
    'stretchtext' => 4
);

// PRINT VARIOUS 1D BARCODES

// CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
$pdf->Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
$pdf->write1DBarcode('CODE 39', 'C39', '', '', '', 18, 0.4, $style, 'N');

// ---------------------------------------------------------

//Close and output PDF document
// $pdf->Output('example_027.pdf', 'I');

$fileatt  = $pdf->Output('tracking_barcode_.pdf', 'E');

// var_dump($doc);die;

$data = chunk_split($fileatt);



include_once(_PS_SWIFT_DIR_.'Swift.php');
include_once(_PS_SWIFT_DIR_.'Swift/Connection/SMTP.php');
include_once(_PS_SWIFT_DIR_.'Swift/Connection/NativeMail.php');
include_once(_PS_SWIFT_DIR_.'Swift/Plugin/Decorator.php');

$id_shop = 1;

if (!$id_shop) {
    $id_shop = Context::getContext()->shop->id;
}

$configuration = Configuration::getMultiple(array(
    'PS_SHOP_EMAIL',
    'PS_MAIL_METHOD',
    'PS_MAIL_SERVER',
    'PS_MAIL_USER',
    'PS_MAIL_PASSWD',
    'PS_SHOP_NAME',
    'PS_MAIL_SMTP_ENCRYPTION',
    'PS_MAIL_SMTP_PORT',
    'PS_MAIL_TYPE'
), null, null, $id_shop);

$from = $configuration['PS_SHOP_EMAIL'];
$from_name = '';
$connection = new Swift_Connection_SMTP(
    $configuration['PS_MAIL_SERVER'],
    $configuration['PS_MAIL_SMTP_PORT'],
    $configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl' ? Swift_Connection_SMTP::ENC_SSL : (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls' ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF))
);
$connection->setTimeout(4);

if (!$connection) {
    echo false;die;
}
if (!empty($configuration['PS_MAIL_USER'])) {
    $connection->setUsername($configuration['PS_MAIL_USER']);
}
if (!empty($configuration['PS_MAIL_PASSWD'])) {
    $connection->setPassword($configuration['PS_MAIL_PASSWD']);
}

$swift = new Swift($connection, Configuration::get('PS_MAIL_DOMAIN', null, null, $id_shop));

$subject = '['.Configuration::get('PS_SHOP_NAME', null, null, $id_shop).'] Tracking barcode';
$message = new Swift_Message($subject);

$message->setCharset('utf-8');


$message->attach(new Swift_Message_Attachment($data, 'tst.pdf'));

        $to_list = new Swift_RecipientList();

        $to_list->addTo('tst@gmail.com', 'tst');
$send = $swift->send($message, $to_list, new Swift_Address($from, $from_name));
$swift->disconnect();

使用TCPDF发送电子邮件附件

电子邮件随附件发送,但我无法用pdf查看器打开文件。

共有2个答案

令狐凌
2023-03-14

您还可以尝试使用以下代码。

$html = 'HTML_CONTENT_FOR_PDF';

$dompdf = new DOMPDF();
$html = utf8_decode($html);
$dompdf->load_html($html);
$dompdf->render();
file_put_contents(FILE_PATH_WITH_NAME, $dompdf->output());

And use the following code to attach it to an email:

$attachment = array(
  'content' => Tools::file_get_contents($file_path),
  'name' => FILE_NAME_FOR_ATTACHMENT,
  'mime' => 'application/pdf'
);

Mail::Send($id_lang,
    TEMPLATE_NAME,
    SUBJECT_TEXT, $template_vars, $customer->email,
    $customer->firstname.' '.$customer->lastname,
    Configuration::get('PS_SHOP_EMAIL'),
    Configuration::get('PS_SHOP_NAME'), $attachment, null
    )
);
訾朗
2023-03-14

下面是工作代码:

<?php
//============================================================+
// File name   : example_001.php
// Begin       : 2008-03-04
// Last Update : 2013-05-14
//
// Description : Example 001 for TCPDF class
//               Default Header and Footer
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com LTD
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: Default Header and Footer
 * @author Nicola Asuni
 * @since 2008-03-04
 */

 require_once(dirname(__FILE__).'/../config/config.inc.php');
 require_once(dirname(__FILE__).'/../init.php');


// Include the main TCPDF library (search for installation path).
// require_once('tcpdf_include.php');
require_once(dirname(__FILE__).'/../tools/tcpdf/config/tcpdf_config.php');
require_once(dirname(__FILE__).'/../tools/tcpdf/tcpdf.php');


// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 027');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
// $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 027', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set a barcode on the page footer
$pdf->setBarcode(date('Y-m-d H:i:s'));

// set font
$pdf->SetFont('helvetica', '', 11);

// add a page
$pdf->AddPage();

// print a message
$txt = "You can also export 1D barcodes in other formats (PNG, SVG, HTML). Check the examples inside the barcodes directory.\n";
$pdf->MultiCell(70, 50, $txt, 0, 'J', false, 1, 125, 30, true, 0, false, true, 0, 'T', false);
$pdf->SetY(30);

// -----------------------------------------------------------------------------

$pdf->SetFont('helvetica', '', 10);

// define barcode style
$style = array(
    'position' => '',
    'align' => 'C',
    'stretch' => false,
    'fitwidth' => true,
    'cellfitalign' => '',
    'border' => true,
    'hpadding' => 'auto',
    'vpadding' => 'auto',
    'fgcolor' => array(0,0,0),
    'bgcolor' => false, //array(255,255,255),
    'text' => true,
    'font' => 'helvetica',
    'fontsize' => 8,
    'stretchtext' => 4
);

// PRINT VARIOUS 1D BARCODES

// CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
$pdf->Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
$pdf->write1DBarcode('CODE 39', 'C39', '', '', '', 18, 0.4, $style, 'N');

// ---------------------------------------------------------

$filename = "tracking_barcode.pdf";
$file_attachement['content'] =$pdf->Output($filename, 'S');
$file_attachement['name'] = 'tst1.pdf';
$file_attachement['mime'] = 'application/pdf';
//Close and output PDF document
// $pdf->Output('example_027.pdf', 'I');

// $data  = $pdf->Output('tracking_barcode_.pdf', 'E');


// $fileatt = $pdf->Output($filename, 'F');

// $data = chunk_split( base64_encode(file_get_contents($filename)) );

// var_dump($doc);die;

// $data = chunk_split($data);



include_once(_PS_SWIFT_DIR_.'Swift.php');
include_once(_PS_SWIFT_DIR_.'Swift/Connection/SMTP.php');
include_once(_PS_SWIFT_DIR_.'Swift/Connection/NativeMail.php');
include_once(_PS_SWIFT_DIR_.'Swift/Plugin/Decorator.php');

$id_shop = 1;

if (!$id_shop) {
    $id_shop = Context::getContext()->shop->id;
}

$configuration = Configuration::getMultiple(array(
    'PS_SHOP_EMAIL',
    'PS_MAIL_METHOD',
    'PS_MAIL_SERVER',
    'PS_MAIL_USER',
    'PS_MAIL_PASSWD',
    'PS_SHOP_NAME',
    'PS_MAIL_SMTP_ENCRYPTION',
    'PS_MAIL_SMTP_PORT',
    'PS_MAIL_TYPE'
), null, null, $id_shop);

$from = $configuration['PS_SHOP_EMAIL'];
$from_name = '';
$connection = new Swift_Connection_SMTP(
    $configuration['PS_MAIL_SERVER'],
    $configuration['PS_MAIL_SMTP_PORT'],
    $configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl' ? Swift_Connection_SMTP::ENC_SSL : (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls' ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF))
);
$connection->setTimeout(4);

if (!$connection) {
    echo false;die;
}
if (!empty($configuration['PS_MAIL_USER'])) {
    $connection->setUsername($configuration['PS_MAIL_USER']);
}
if (!empty($configuration['PS_MAIL_PASSWD'])) {
    $connection->setPassword($configuration['PS_MAIL_PASSWD']);
}

$swift = new Swift($connection, Configuration::get('PS_MAIL_DOMAIN', null, null, $id_shop));

$subject = '['.Configuration::get('PS_SHOP_NAME', null, null, $id_shop).'] Tracking barcode';
$message = new Swift_Message($subject);

$message->setCharset('utf-8');


$message->attach(new Swift_Message_Attachment(
    $file_attachement['content'],
    $file_attachement['name'],
    $file_attachement['mime']
    ));

$to_list = new Swift_RecipientList();

$to_list->addTo('ttt@gmail.com', 'ttt');
$send = $swift->send($message, $to_list, new Swift_Address($from, $from_name));
$swift->disconnect();
 类似资料:
  • 我想用mailto标签发送电子邮件,附带一个pdf文件作为附件。mailto标记使用以下方法打开邮件窗口,其中包含传递的参数,如to和subject: 但是,附件作为一个参数不起作用。请建议如何在手机中发送pdf附件。 谢啦

  • 我正在用cakephp开发一个应用程序,我需要在其中生成一个pdf并将其作为电子邮件附件发送。我已经能够使用dompdf创建pdf视图,但我不知道如何将其保存为文件并以电子邮件附件的形式发送。请告诉我如何做到这一点。

  • 我有一个谷歌表单,有两个表单,表单回复和报告:见这里 当表单提交报表时,更改响应表的最后一个表单或最后一行。我想发送一封电子邮件给提交表格的人,并在发送电子邮件后将报告表作为PDF附加在e栏中:请参阅此处 电子邮件发送至:表格回复栏b主题:一些文本和表格回复栏a抄送:ABC@yahoo.com正文:部分文本和表格答复f列附件:PDF格式的报告表 我用了这个代码但不管用 我从电子表格中获取url 我

  • 问题内容: 我创建了一个函数来获取当前网页的屏幕截图,并使用html2canvas和jsPDF将其保存为PDF。以下是我的代码: 通过使用上面的代码,我可以下载文件并将其保存在本地。但我想直接使用php脚本通过电子邮件发送生成的pdf。 以下是在php脚本中发布图像的代码,该图像将作为电子邮件发送: 但是如何在参数中发布生成的pdf ? 请为此提出任何解决方案。 问题答案: 请检查代码- 我希望它

  • 我正在使用TCPDF库在我的CakePHP应用程序中创建发票。如果我转到,我的view.ctp包括在浏览器中生成和显示pdf的代码。我想添加发送PDF作为电子邮件附件的能力,所以我创建了动作,并将代码从view.ctp复制到电子邮件模板中。 现在我被卡住了,我不知道如何在附加PDF之前先生成它。在我的view.ctp页面模板的末尾,我使用 它将pdf发送到浏览器中查看,而不创建文件。如果使用“F”

  • 问题内容: 我正在使用sendgrid发送电子邮件,并且使用以下代码可以正常工作,但没有附件。 但是我需要发送附件,因此我搜索了github源和Web文档API,由于某种原因,没有javadocs,但是有一个示例GitHub sendgrid, 所以我一直在尝试直到它起作用为止,我缩小了一些异常和响应代码,起初我是被禁止的未经授权,最好是响应202,表示有效且已排队(在此处检查),这是我的代码发送