As the title says I tried to output PDF using the ** TCPDF ** and ** FPDI ** libraries. This time I will execute it from the command line.
Please prepare an environment where PHP and Composer can be used as you like.
Docker The Docker environment I used this time is as follows (partially omitted).
yml:./docker-compose.yml
version: '3'
services:
php:
build: ./php
volumes:
- ./work:/home/work
./php/dockerfile
FROM php:7.3-fpm
COPY php.ini /usr/local/etc/php/
# Composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php -r "if (hash_file('sha384', 'composer-setup.php') === '795f976fe0ebd8b75f26a6dd68f78fd3453ce79f32ecb33e7fd087d39bfeb978342fb73ac986cd4f54edd0dc902601dc') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php \
&& php -r "unlink('composer-setup.php');" \
&& mv composer.phar /usr/local/bin/composer
ini:./php/php.ini
date.timezone = "Asia/Tokyo"
Composer When the launch is complete, enter the php container and Install the library in Composer.
In addition to the library, the description of autoload is also added.
/home/work/src/composer.json
{
"require": {
"setasign/fpdi": "^2.3",
"tecnickcom/tcpdf": "^6.3"
},
"autoload": {
"psr-4": {
"Classes\\" : "classes/"
}
}
}
Once the installation is complete, you're done.
# cd /home/work/src
# composer install
# cd /home/work/
# ls /home/work/output/pdf/fileYYYYmmdd-HHiiss #PDF output file
# ls /home/work/src/resources/font/ipam.ttf #Font storage location
# ls /home/work/src/resources/pdf/templete.pdf #PDF template storage location
# ls /home/work/bin/make-pdf.php #Source to run on the command line
# ls /home/work/src/bootstrap.php #Start-up file(Omitted)
# ls /home/work/src/classes/MyTcpdf.php #TCPDF usage class
# ls /home/work/src/classes/common/DynamicProperty.php #Dynamic class(Omitted)
# ls /home/work/src/classes/MyTcpdf/FontColor.php #Font color class(Omitted)
It's a very simple PDF template file.
/home/work/bin/make-pdf.php
<?php
require_once __DIR__ . '/../src/bootstrap.php';
use Classes\MyTcpdf;
use Classes\MyTcpdf\FontColor;
//Output without using a template
$myTcpdf = new MyTcpdf();
$myTcpdf->addPage();
$myTcpdf->setText('Output without using a template', 10, 20, new FontColor(0, 255, 0));
$myTcpdf->outputPdfFile();
//Output using template
$myTcpdfTmp = new MyTcpdf();
$myTcpdfTmp->addPageWithTemplete(1);
$myTcpdfTmp->setText('Output using template', 10, 20, new FontColor(255, 0, 0));
$myTcpdfTmp->outputPdfFile();
/home/work/src/classes/MyTcpdf.php
<?php
namespace Classes;
use TCPDF_FONTS;
use setasign\Fpdi\Tcpdf\Fpdi;
use Classes\MyTcpdf\FontColor;
class MyTcpdf
{
//Output file
const OUTPUT_FORMAT_CHAR = 'F';
const OUTPUT_FILE_DIR = __DIR__.'/../../output/pdf/';
const OUTPUT_FILE_NAME = 'file';
// PDF
const PDF_ORIENTATION = 'P';
const PDF_UNIT = 'mm';
const PDF_SIZE = 'A4';
const PDF_TEMPLETE = __DIR__.'/../resources/pdf/templete.pdf';
//Font file
const FONT_FILE_NAME = __DIR__.'/../resources/font/ipam.ttf';
/**
*constructor
*/
public function __construct()
{
$this->fpdi = new Fpdi(self::PDF_ORIENTATION, self::PDF_UNIT, self::PDF_SIZE);
$this->fpdi->SetMargins(0, 0, 0);
$this->fpdi->SetFont($this->getFont(), '', 32);
}
/**
*Set the output character
* @param _text string
* @param _x X coordinates
* @param _y Y coordinates
* @param _h height
*/
public function setText(string $_text, int $_x, int $_y, FontColor $_fontColor, int $_h = 0)
{
$this->fpdi->SetTextColor($_fontColor->R, $_fontColor->G, $_fontColor->B);
$this->fpdi->SetXY($_x, $_y);
$this->fpdi->Write($_h, $_text);
}
/**
*Get the font to add
*/
private function getFont()
{
return TCPDF_FONTS::addTTFfont(self::FONT_FILE_NAME);
}
/**
*Get the output file name
* @return string filename
*/
private function getOutputFilename()
{
return self::OUTPUT_FILE_DIR . self::OUTPUT_FILE_NAME . date("Ymd-His"). '.pdf';
}
/**
*Add page
*/
public function addPage()
{
$this->fpdi->AddPage();
}
/**
*Add a template-based page
*/
public function addPageWithTemplete($_pageNum)
{
$this->fpdi->SetSourceFile(self::PDF_TEMPLETE);
$page = $this->fpdi->importPage($_pageNum);
$this->fpdi->AddPage();
$this->fpdi->useTemplate($page, null, null, null, null, true);
}
/**
*PDF file output
*/
public function outputPdfFile()
{
$this->fpdi->Output(
$this->getOutputFilename(),
self::OUTPUT_FORMAT_CHAR
);
}
}
php /home/work/bin/make-pdf.php
The file was output as follows.
No template | There is a template |
---|---|
Since you can output characters on the PDF template, you can output only the necessary information without changing the format. It seems that it can be used for files such as rosters, invoices, receipts, etc. where only specific parts change.
Recommended Posts