บทนี้เราจะมาพูดถึงกันเรื่องการทำหัวกระดาษ ตัว TCPDF เขาจัดเลย์เอ้าของหัวกระดาษไว้ให้เราแล้ว โดยเราเพียงแค่ส่งโลโกเข้าไป ขนาดโลโก้เข้าไป ข้อความที่จะให้พิมพ์ เราก็จะได้หัวกระดาษแบบดีฟอลต์มา
ลองดูตัวอย่างนี้
<?php
require_once('vendor/autoload.php');
// create new PDF document
$pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set header
$pdf->setPrintHeader(true);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// Add a page
$pdf->AddPage();
// Print text
$pdf->WriteHTML("A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine. I am so happy, my dear friend, so absorbed in the exquisite sense of mere tranquil existence, that I neglect my talents. I should be incapable of drawing a single stroke at the present moment; and yet I feel that I never was a greater artist than now.");
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('hello-tcpdf.pdf', 'I');
$pdf->Close();
หน้าตาจะออกมาประมาณนี้

คำสั่งที่มีผลต่อหัวกระดาษคือคำสั่ง
// set header
$pdf->setPrintHeader(true);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setPrintHeader(true);
จะเป็นตัวบอกว่าให้ปริ้นต์หัวกระดาษออกมาด้วย ตรงนี้ถ้าเซ็ตเป็น false ตรงคำสั่ง SetHeaderData ก็จะไม่มีผล
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
กั้นหัวกระดาษไว้เท่าไร
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
ดาต้าที่พิมพ์ลงไปบนหัวกระดาษ logo อะไร, ความกว้างของโลโก้เท่าไหร่, ข้อความ Title และข้อความบรรทัดถัดลงมาจาก Title อะไร
ทีนี้ผมลองเปลี่ยนให้เรียกใช้โลโกผมเองดูมั่ง
<?php
define('K_PATH_IMAGES', dirname(__FILE__).'/images/');
require_once('vendor/autoload.php');
// create new PDF document
$pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set header
$pdf->setPrintHeader(true);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetHeaderData('mym.png', 18, 'Select2Web inc.', "Contact Mobile: 0875412545\nEmail: [email protected]");
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// Add a page
$pdf->AddPage();
// Print text
$pdf->WriteHTML("A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine. I am so happy, my dear friend, so absorbed in the exquisite sense of mere tranquil existence, that I neglect my talents. I should be incapable of drawing a single stroke at the present moment; and yet I feel that I never was a greater artist than now.");
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('hello-tcpdf.pdf', 'I');
$pdf->Close();

ข้อที่ต้องสังเกตุในโค้ดคือ
ผมเพิ่มตัวแปร constant K_PATH_IMAGES เข้ามาที่หัวไฟล์
define('K_PATH_IMAGES', dirname(__FILE__).'/images/');
ตัวแปร K_PATH_IMAGES นี้ไว้สำหรับระบุพาธโฟลเดอร์รูปภาพ และตัวแปรนี้จะต้องอยู่ก่อนคำสั่ง require_once(‘vendor/autoload.php’); เท่านั้น
ผมสามารถใช้ \n เพื่อขึ้นบรรทัดใหม่ข้อความได้ แล้วก็ต้องคร่อมข้อความด้วยเครื่องหมาย สองเขา (double qoute)
$pdf->SetHeaderData('mym.png', 18, 'Select2Web inc.', "Contact Mobile: 0875412545\nEmail: [email protected]");
ในบทถัดไปผมจะพูดถึงเรื่องการทำ custom header เพื่อให้สามารถแก้ layout ของหัวกระดาษได้ หัวกระดาษในบทนี้เขาจัดเลย์เอ้าไว้ให้เราแล้วถ้าเราจะแก้ก็ต้อง ติดตามตอนต่อไปครับ
Leave a Reply