簡體   English   中英

每頁將2張圖片插入pdf文件

[英]inserting 2 images into a pdf on each page

我有一個PHP腳本,當他們通過貝寶(Paypal)完成付款時,會通過電子郵件將2張圖片發送給他人。 現在這些圖像中的1個通過imagettftext函數添加了文本。

現在,我要做的是,不是通過電子郵件發送2張圖像,而是希望將2張圖像添加到其各自頁面上的PDF文件中,然后將該PDF文件通過電子郵件發送給他們。

這是我發送圖片的電子郵件的代碼:

$sql = mysql_query("select * from `bookings` where `id`='$order_id'");

while ($row = mysql_fetch_assoc($sql))
{
    foreach ($row as $k => $v)
        $$k = $v;

    $img = imagecreatefrompng("ticket-blank.png");

    if (!empty($lead_home_phone)) $phone = "Home Phone: $lead_home_phone\n";
    else if (!empty($lead_work_phone)) $phone = "Work Phone: $lead_work_phone\n";
    else if (!empty($lead_mobile_phone)) $phone = "Mobile Phone: $lead_mobile_phone\n";

    $dets = "Full name: $lead_title $lead_name\n";
    $dets .= "Address 1: $lead_address_1\n";
    $dets .= "Postcode: $lead_postcode\n";
    $dets .= "City/Town: $lead_city\n";
    $dets .= "State/Province: $lead_state\n";
    $dets .= "Country: $lead_country\n";
    if (!empty($phone)) $dets .= "$phone\n";
    $dets .= "Email Address: $lead_email";

    imagettftext($img, 8, 0, 50, 115, imagecolorallocate($img, 0, 0, 0), 'arial.ttf', "BOOKING FOR ".date('F j, Y', $the_date)."\nORDER ID # $order_id");
    imagettftext($img, 8, 0, 270, 115, imagecolorallocate($img, 0, 0, 0), 'arial.ttf', "$num_adults ADULTS $num_children CHILDREN\n$ticket_type");
    imagettftext($img, 8, 0, 50, 165, imagecolorallocate($img, 0, 0, 0), 'arial.ttf', $dets);
    imagettftext($img, 8, 0, 415, 165, imagecolorallocate($img, 0, 0, 0), 'arial.ttf', "Total: CZK $pay_total");

    $img_name = time().'.png';

    imagepng($img, "tickets/$img_name");

    $files = array();
    $files []= "tickets/$img_name";
    $files []= "tickets/brochure.jpg";

    // email fields: to, from, subject, and so on
    $from = "info@mysite.com"; 
    $subject ="Your Ticket"; 

    $message = "Booking Complete!";

    $headers = "From: $from";

    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

    // multipart boundary 
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $message .= "--{$mime_boundary}\n";

    // preparing attachments
    for ($x=0; $x < count($files); $x++)
    {
        $file = fopen($files[$x],"rb");
        $data = fread($file,filesize($files[$x]));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
        "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }

    // mail the emails with attachments
    mail('me@myemail.com', $subject, $message, $headers); 
}

如何將2張圖片添加到PDF文件中,然后通過電子郵件發送?

看看像fpdf這樣的php pdf生成庫。 我以前使用過fpdf,發現它非常簡單直接。 這是一個如何在他們的網站上使用它的示例:

<?php
require('fpdf.php');

class PDF extends FPDF
{
// Page header
function Header()
{
    // Logo
    $this->Image('logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Title
    $this->Cell(30,10,'Title',1,0,'C');
    // Line break
    $this->Ln(20);
}

// Page footer
function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++)
    $pdf->Cell(0,10,'Printing line number '.$i,0,1);
$pdf->Output();
?>

如您所見,您基本上只需要創建一個pdf文件,添加一個頁面,然后將圖像,文本,表格等添加到該頁面,然后您就可以輸出pdf了。 在您的情況下,您希望將其輸出到一些臨時文件,通過電子郵件發送該文件,然后刪除該文件以節省服務器空間(或在幾天后刪除它們)。

希望這會有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM