PHP

Complete Guide to Generating PDFs in PHP: How to Output Japanese Characters Using the FPDF Library

How to generate PDF files using PHP is particularly useful in business scenarios for document processing and data report creation. In this article, we will provide a detailed explanation of how to use the widely-used library “FPDF” for generating PDFs with PHP, making it easy to understand even for beginners. This article will cover everything from the basic usage of FPDF, which supports Japanese characters, to the simple process of generating a PDF.

What is FPDF?

FPDF is a class library for generating PDF documents in PHP scripts. Since it allows you to generate PDFs using only PHP without the need for external PDF libraries, it is very easy to implement. Additionally, FPDF is free to use and offers many features, making it possible to customize and generate various PDF documents.

Download FPDF and Japanese Language Support Library

First, download the FPDF main package and the additional library that supports Japanese. Follow the steps below to download them.

  1. Access the official FPDF website (FPDF).
  2. From FPDF, download the Japanese version (latest version in Japanese) ZIP file from the “Downloads” link on the left menu.

Unpacking the Library Downloaded with FPDF

Unpack the downloaded ZIP file and place it in the appropriate directory of your project. This process is very simple, and once unpacked, you can start using FPDF functions from your PHP script.
Unpacking Image
Unpack the files as shown in the image above.

Basic Code for Generating PDFs

The basic PHP code (pdfoutput.php) for generating PDF output using FPDF is as follows:

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

	$pdf=new PDF_Japanese();
	$pdf->AddSJISFont();
	$pdf->Open();
	$pdf->AddPage();
	$pdf->SetFont('SJIS','',20);
	$pdf->Write(12,'Using FPDF to output Japanese characters');
	$pdf->Ln();
	$pdf->Write(10,'This is a PHP program to output in pdf.');
	$pdf->Output();
?>

This code generates a PDF containing Japanese characters and outputs it directly to the browser. The Write method is used to add text, and the Ln method is used to insert a line break. The text breaks at line 9.

FPDF: Demo Page of PHP Program to Output Japanese Characters in PDF

By running the PHP program created with FPDF, you can experience its convenience. For example, you can use FPDF to generate formatted PDFs such as purchase orders and receipts. Also, by visiting the following demo page (FPDF: Demo of PHP Program to Output Japanese Characters in PDF), you can see how the PDF is generated in practice.
FPDF: Demo Page of PHP Program to Output Japanese Characters in PDF

Source: FPDF

FPDF
It seems that it can be used for formatted PDF output, such as purchase orders and receipts.

Conclusion

By using the FPDF library, it is possible to flexibly generate PDF documents using only PHP. Through this article, we have learned from the basic usage of FPDF to how to output PDFs in Japanese. Continue to use FPDF to generate various PDF documents.
I hope this article serves as a valuable resource for engineers and beginners interested in PDF generation. Using FPDF for PDF generation will be a great help in your projects or business scenarios.

 
※Please use at your own risk if you intend to copy this content.