Use PHP to create PDF documents

Source: Internet
Author: User
I used FPDF (www.fpdf.org) to create a PDF document in PHP. after downloading the fpdf class library, I also needed to use the following Chinese class library to support Chinese, however, only one Chinese font can be used ). For this reason, I have been worried for a long time, and now I have finally finished it. I want to convert the TrueType font to the pt1 font:

The following is a Chinese library found on FPDF:
Require ('fpdf. php ');

$ Big5_widths = array (''=> 250 ,'! '=> 250,' "'=> 408,' # '=> 668,' $ '=> 490,' % '=> 875,' & '=> 698, ''' => 250,
'(' => 240, ')' => 240, '*' => 417, '+' => 667, ',' => 250, '-' => 313 ,'. '=> 250,'/'=> 520, '0' => 500, '1' => 500,
'2' => 500, '3' => 500, '4' => 500, '5' => 500, '6' => 500, '7' => 500, '8' => 500, '9' => 500, ':' => 250, ';' => 250,
'<' => 667, '=> 667,'> '=> 667 ,'? '=> 396,' @ '=> 921, 'a' => 677,' B '=> 615, 'C' => 719, 'D' => 760, & apos; E & apos; = & apos; 625 & apos,
'F' => 552, 'G' => 771, 'H' => 802, 'I' => 354, 'J' => 354, 'K' => 781, 'L' => 604, 'M' => 927, 'n' => 750, 'O' => 823,
'P' => 563, 'q' => 823, 'R' => 729, 's' => 542, 'T' => 698, 'U' => 771, 'V' => 729, 'W' => 948, 'x' => 771, 'y' => 677,
'Z' => 635, '[' => 344, ''=> 520, ']' => 344, '^' => 469, '_' => 500, ''' => 250, 'A '=> 469,' B '=> 521, 'C' => 427,
'D' => 521, 'E' => 438, 'F' => 271, 'G' => 469, 'H' => 531, 'I => 250, 'J' => 250, 'K' => 458, 'L' => 240, 'M' => 802,
'N' => 531, 'O' => 500, 'P' => 521, 'q' => 521, 'R' => 365,'s '=> 333, 'T' => 292, 'u' => 521, 'V' => 458, 'W' => 677,
'X' => 479, 'y' => 458, 'Z' => 427, '{' => 480, '|' => 496, '}' => 480 ,'~ '=> 667 );

$ GB_widths = array (''=> 207 ,'! '=> 270,' "'=> 342,' # '=> 467,' $ '=> 462,' % '=> 797,' & '=> 710, ''' => 239,
'(' => 374, ')' => 374, '*' => 423, '+' => 605, ',' => 238, '-' => 375 ,'. '=> 238,'/'=> 334, '0' => 462, '1' => 462,
'2' => 462, '3' => 462, '4' => 462, '5' => 462, '6' => 462, '7' => 462, '8' => 462, '9' => 462, ':' => 238, ';' => 238,
'<' => 605, '=> 605,'> '=> 605 ,'? '=> 344,' @ '=> 748, 'a' => 684,' B '=> 560, 'C' => 695, 'D' => 739, & apos; E & apos; = & apos; 563 & apos,
'F' => 511, 'G' => 729, 'H' => 793, 'I' => 318, 'J' => 312, 'K' => 666, 'L' => 526, 'M' => 896, 'n' => 758, 'O' => 772,
'P' => 544, 'q' => 772, 'R' => 628, 's' => 465, 'T' => 607, 'U' => 753, 'V' => 711, 'W' => 972, 'x' => 647, 'y' => 620,
'Z' => 607, '[' => 374, ''=> 333, ']' => 374, '^' => 606, '_' => 500, ''' => 239, 'A '=> 417,' B '=> 503, 'C' => 427,
'D' => 529, 'E' => 415, 'F' => 264, 'G' => 444, 'H' => 518, 'I => 241, 'J' => 230, 'K' => 495, 'L' => 228, 'M' => 793,
'N' => 527, 'O' => 524, 'P' => 524, 'q' => 504, 'R' => 338,'s '=> 336, 'T' => 277, 'u' => 517, 'V' => 450, 'W' => 652,
'X' => 466, 'y' => 452, 'Z' => 407, '{' => 370, '|' => 258, '}' => 370 ,'~ '=> 605 );

Class performance_chinese extends FPDF
{
Function AddCIDFont ($ family, $ style, $ name, $ cw, $ CMap, $ registry)
{
$ I = count ($ this-> fonts) + 1;
$ Fontkey = strtolower ($ family). strtoupper ($ style );
$ This-> fonts [$ fontkey] = array ('I' => $ I, 'type' => 'type0', 'name' => $ name, 'up' =>-120, 'ut' => 40, 'cw '=> $ cw, 'cmap' => $ CMap, 'registry' => $ registry );
}

Function AddBig5Font ($ family = 'big5 ')
{
$ Cw = $ GLOBALS ['big5 _ widths'];
$ Name = 'msungstd-Light-Acro ';
$ CMap = 'etenms-B5-H ';
$ Registry = array ('ordering' => 'cns1', 'support' => 0 );
$ This-> AddCIDFont ($ family, '', $ name, $ cw, $ CMap, $ registry );
$ This-> AddCIDFont ($ family, 'B', $ name. ', Bold', $ cw, $ CMap, $ registry );
$ This-> AddCIDFont ($ family, 'I', $ name. ', Italic', $ cw, $ CMap, $ registry );
$ This-> AddCIDFont ($ family, 'bi', $ name. ', BoldItalic', $ cw, $ CMap, $ registry );
}

Function AddGBFont ($ family = 'GB ')
{
$ Cw = $ GLOBALS ['GB _ widths'];
$ Name = 'stsongstd-Light-Acro ';
$ CMap = 'gbkp-EUC-H ';
$ Registry = array ('ordering' => 'gb1', 'support' => 2 );
$ This-> AddCIDFont ($ family, '', $ name, $ cw, $ CMap, $ registry );
$ This-> AddCIDFont ($ family, 'B', $ name. ', Bold', $ cw, $ CMap, $ registry );
$ This-> AddCIDFont ($ family, 'I', $ name. ', Italic', $ cw, $ CMap, $ registry );
$ This-> AddCIDFont ($ family, 'bi', $ name. ', BoldItalic', $ cw, $ CMap, $ registry );
}

Function GetStringWidth ($ s)
{
If ($ this-> CurrentFont ['type'] = 'type0 ')
Return $ this-> GetMBStringWidth ($ s );
Else
Return parent: GetStringWidth ($ s );
}

Function GetMBStringWidth ($ s)
{
// Multi-byte version of GetStringWidth ()
$ L = 0;
$ Cw = & $ this-> CurrentFont ['cw '];
$ Nb = strlen ($ s );
$ I = 0;
While ($ I <$ nb)
{
$ C = $ s [$ I];
If (ord ($ c) <128)
{
$ L + = $ cw [$ c];
$ I ++;
}
Else
{
$ L + = 1000;
$ I + = 2;
}
}
Return $ l * $ this-> FontSize/1000;
}

Function MultiCell ($ w, $ h, $ txt, $ border = 0, $ align = 'l', $ fill = 0)
{
If ($ this-> CurrentFont ['type'] = 'type0 ')
$ This-> MBMultiCell ($ w, $ h, $ txt, $ border, $ align, $ fill );
Else
Parent: MultiCell ($ w, $ h, $ txt, $ border, $ align, $ fill );
}

Function MBMultiCell ($ w, $ h, $ txt, $ border = 0, $ align = 'l', $ fill = 0)
{
// Multi-byte version of MultiCell ()
$ Cw = & $ this-> CurrentFont ['cw '];
If ($ w = 0)
$ W = $ this-> w-$ this-> rMargin-$ this-> x;
$ Wmax = ($ W-2 * $ this-> cMargin) * 1000/$ this-> FontSize;
$ S = str_replace ("\ r", '', $ txt );
$ Nb = strlen ($ s );
If ($ nb> 0 and $ s [$ nb-1] = "\ n"
$ Nb --;
$ B = 0;
If ($ border)
{
If ($ border = 1)
{
$ Border = 'ltrb ';
$ B = 'lrt ';
$ B2 = 'LR ';
}
Else
{
$ B2 = '';
If (is_int (strpos ($ border, 'L ')))
$ B2. = 'l ';
If (is_int (strpos ($ border, 'r ')))
$ B2. = 'R ';
$ B = is_int (strpos ($ border, 'T '))? $ B2. 'T': $ b2;
}
}
$ Sep =-1;
$ I = 0;
$ J = 0;
$ L = 0;
$ Ns = 0;
$ Nl = 1;
While ($ I <$ nb)
{
// Get next character
$ C = $ s [$ I];
// Check if ASCII or MB
$ Ascii = (ord ($ c) <128 );
If ($ c = "\ n"
{
// Explicit line break
If ($ this-> ws> 0)
{
$ This-> ws = 0;
$ This-> _ out ('0 Tw ');
}
$ This-> Cell ($ w, $ h, substr ($ s, $ j, $ I-$ j), $ B, 2, $ align, $ fill );
$ I ++;
$ Sep =-1;
$ J = $ I;
$ L = 0;
$ Ns = 0;
$ Nl ++;
If ($ border and $ nl = 2)
$ B = $ b2;
Continue;
}
If (! $ Ascii)
{
$ Sep = $ I;
$ Ls = $ l;
}
Elseif ($ c = '')
{
$ Sep = $ I;
$ Ls = $ l;
$ Ns ++;
}
$ L + = $ ascii? $ Cw [$ c]: 1000;
If ($ l> $ wmax)
{
// Automatic line break
If ($ sep =-1 or $ I = $ j)
{
If ($ I = $ j)
$ I + = $ ascii? 1: 2;
If ($ this-> ws> 0)
{
$ This-> ws = 0;
$ This-> _ out ('0 Tw ');
}
$ This-> Cell ($ w, $ h, substr ($ s, $ j, $ I-$ j), $ B, 2, $ align, $ fill );
}
Else
{
If ($ align = 'J ')
{
If ($ s [$ sep] = '')
$ Ns --;
If ($ s [$ i-1] = '')
{
$ Ns --;
$ Ls-= $ cw [''];
}
$ This-> ws = ($ ns> 0 )? ($ Wmax-$ ls)/1000 * $ this-> FontSize/$ ns: 0;
$ This-> _ out (sprintf ('%. 3f Tw', $ this-> ws * $ this-> k ));
}
$ This-> Cell ($ w, $ h, substr ($ s, $ j, $ sep-$ j), $ B, 2, $ align, $ fill );
$ I = ($ s [$ sep] = '')? $ Sep + 1: $ sep;
}
$ Sep =-1;
$ J = $ I;
$ L = 0;
$ Ns = 0;
$ Nl ++;
If ($ border and $ nl = 2)
$ B = $ b2;
}
Else
$ I + = $ ascii? 1: 2;
}
// Last chunk
If ($ this-> ws> 0)
{
$ This-> ws = 0;
$ This-> _ out ('0 Tw ');
}
If ($ border and is_int (strpos ($ border, 'B ')))
$ B. = 'B ';
$ This-> Cell ($ w, $ h, substr ($ s, $ j, $ I-$ j), $ B, 2, $ align, $ fill );
$ This-> x = $ this-> lMargin;
}

Function Write ($ h, $ txt, $ link = '')
{
If ($ this-> CurrentFont ['type'] = 'type0 ')
$ This-> MBWrite ($ h, $ txt, $ link );
Else
Parent: Write ($ h, $ txt, $ link );
}

Function MBWrite ($ h, $ txt, $ link)
{
// Multi-byte version of Write ()
$ Cw = & $ this-> CurrentFont ['cw '];
$ W = $ this-> w-$ this-> rMargin-$ this-> x;
$ Wmax = ($ W-2 * $ this-> cMargin) * 1000/$ this-> FontSize;
$ S = str_replace ("\ r", '', $ txt );
$ Nb = strlen ($ s );
$ Sep =-1;
$ I = 0;
$ J = 0;
$ L = 0;
$ Nl = 1;
While ($ I <$ nb)
{
// Get next character
$ C = $ s [$ I];
// Check if ASCII or MB
$ Ascii = (ord ($ c) <128 );
If ($ c = "\ n"
{
// Explicit line break
$ This-> Cell ($ w, $ h, substr ($ s, $ j, $ I-$ j), 0, 2, '', 0, $ link );
$ I ++;
$ Sep =-1;
$ J = $ I;
$ L = 0;
If ($ nl = 1)
{
$ This-> x = $ this-> lMargin;
$ W = $ this-> w-$ this-> rMargin-$ this-> x;
$ Wmax = ($ W-2 * $ this-> cMargin) * 1000/$ this-> FontSize;
}
$ Nl ++;
Continue;
}
If (! $ Ascii or $ c = '')
$ Sep = $ I;
$ L + = $ ascii? $ Cw [$ c]: 1000;
If ($ l> $ wmax)
{
// Automatic line break
If ($ sep =-1 or $ I = $ j)
{
If ($ this-> x> $ this-> lMargin)
{
// Move to next line
$ This-> x = $ this-> lMargin;
$ This-> y + = $ h;
$ W = $ this-> w-$ this-> rMargin-$ this-> x;
$ Wmax = ($ W-2 * $ this-> cMargin) * 1000/$ this-> FontSize;
$ I ++;
$ Nl ++;
Continue;
}
If ($ I = $ j)
$ I + = $ ascii? 1: 2;
$ This-> Cell ($ w, $ h, substr ($ s, $ j, $ I-$ j), 0, 2, '', 0, $ link );
}
Else
{
$ This-> Cell ($ w, $ h, substr ($ s, $ j, $ sep-$ j), 0, 2, '', 0, $ link );
$ I = ($ s [$ sep] = '')? $ Sep + 1: $ sep;
}
$ Sep =-1;
$ J = $ I;
$ L = 0;
If ($ nl = 1)
{
$ This-> x = $ this-> lMargin;
$ W = $ this-> w-$ this-> rMargin-$ this-> x;
$ Wmax = ($ W-2 * $ this-> cMargin) * 1000/$ this-> FontSize;
}
$ Nl ++;
}
Else
$ I + = $ ascii? 1: 2;
}
// Last chunk
If ($ I! = $ J)
$ This-> Cell ($ l/1000 * $ this-> FontSize, $ h, substr ($ s, $ j, $ I-$ j), 0, 0 ,'', 0, $ link );
}

Function _ putfonts ()
{
$ Nf = $ this-> n;
Foreach ($ this-> diffs as $ diff)
{
// Encodings
$ This-> _ newobj ();
$ This-> _ out ('<> ');
$ This-> _ out ('endobj ');
}
$ Mqr = get_magic_quotes_runtime ();
Set_magic_quotes_runtime (0 );
Foreach ($ this-> FontFiles as $ file => $ info)
{
// Font file embedding
$ This-> _ newobj ();
$ This-> FontFiles [$ file] ['n'] = $ this-> n;
If (defined ('fpdf _ fontpath '))
$ File = FPDF_FONTPATH. $ file;
$ Size = filesize ($ file );
If (! $ Size)
$ This-> Error ('font file not found ');
$ This-> _ out ('<If (substr ($ file,-2) = '. z ')
$ This-> _ out ('/Filter/FlateDecode ');
$ This-> _ out ('/Length1'. $ info ['length1 ']);
If (isset ($ info ['length2'])
$ This-> _ out ('/leng2'. $ info ['leng2'].'/Length3 0 ');
$ This-> _ out ('> ');
$ F = fopen ($ file, 'RB ');
$ This-> _ putstream (fread ($ f, $ size ));
Fclose ($ f );
$ This-> _ out ('endobj ');
}
Set_magic_quotes_runtime ($ mqr );
Foreach ($ this-> fonts as $ k => $ font)
{
// Font objects
$ This-> _ newobj ();
$ This-> fonts [$ k] ['n'] = $ this-> n;
$ This-> _ out ('<If ($ font ['type'] = 'type0 ')
$ This-> _ putType0 ($ font );
Else
{
$ Name = $ font ['name'];
$ This-> _ out ('/BaseFont/'. $ name );
If ($ font ['type'] = 'core ')
{
// Standard font
$ This-> _ out ('/Subtype/type1 ');
If ($ name! = 'Symbol 'and $ name! = 'Zapfdingbats ')
$ This-> _ out ('/Encoding/WinAnsiEncoding ');
}
Else
{
// Additional font
$ This-> _ out ('/Subtype/'. $ font ['type']);
$ This-> _ out ('/FirstChar 32 ');
$ This-> _ out ('/LastChar 255 ');
$ This-> _ out ('/widths'. ($ this-> n + 1). '0 r ');
$ This-> _ out ('/FontDescriptor'. ($ this-> n + 2). '0 r ');
If ($ font ['enc'])
{
If (isset ($ font ['diff'])
$ This-> _ out ('/encoding'. ($ nf + $ font ['diff']). '0 r ');
Else
$ This-> _ out ('/Encoding/WinAnsiEncoding ');
}
}
$ This-> _ out ('> ');
$ This-> _ out ('endobj ');
If ($ font ['type']! = 'Core ')
{
// Widths
$ This-> _ newobj ();
$ Cw = & $ font ['cw '];
$ S = '[';
For ($ I = 32; $ I <= 255; $ I ++)
$ S. = $ cw [chr ($ I)]. '';
$ This-> _ out ($ s. ']');
$ This-> _ out ('endobj ');
// Descriptor
$ This-> _ newobj ();
$ S = '<Foreach ($ font ['desc'] as $ k => $ v)
$ S. = '/'. $ k. '. $ v;
$ File = $ font ['file'];
If ($ file)
$ S. = '/fontfile'. ($ font ['type'] = 'type1 '? '': '2').''. $ this-> FontFiles [$ file] ['n']. '0 R ';
$ This-> _ out ($ s. '> ');
$ This-> _ out ('endobj ');
}
}
}
}

Function _ putType0 ($ font)
{
// Type0
$ This-> _ out ('/Subtype/type0 ');
$ This-> _ out ('/BaseFont/'. $ font ['name']. '-'. $ font ['cmap']);
$ This-> _ out ('/Encoding/'. $ font ['cmap']);
$ This-> _ out ('/DescendantFonts ['. ($ this-> n + 1). '0 R] ');
$ This-> _ out ('> ');
$ This-> _ out ('endobj ');
// CIDFont
$ This-> _ newobj ();
$ This-> _ out ('<$ This-> _ out ('/Subtype/cidfonttype0 ');
$ This-> _ out ('/BaseFont/'. $ font ['name']);
$ This-> _ out ('/CIDSystemInfo <> ');
$ This-> _ out ('/FontDescriptor'. ($ this-> n + 1). '0 r ');
$ W = '/W [1 [';
Foreach ($ font ['cw '] as $ w)
$ W. = $ w .'';
$ This-> _ out ($ W. ']');
$ This-> _ out ('> ');
$ This-> _ out ('endobj ');
// Font descriptor
$ This-> _ newobj ();
$ This-> _ out ('<$ This-> _ out ('/FontName/'. $ font ['name']);
$ This-> _ out ('/Flags 6 ');
$ This-> _ out ('/FontBBox [0 0 1000 1000]');
$ This-> _ out ('/ItalicAngle 0 ');
$ This-> _ out ('/Ascent 1000 ');
$ This-> _ out ('/Descent 0 ');
$ This-> _ out ('/CapHeight 1000 ');
$ This-> _ out ('/StemV 10 ');
$ This-> _ out ('> ');
$ This-> _ out ('endobj ');
}
}
?>

Save the above code as chinese. php and reference it. However, you can only get one font. To support all Chinese fonts, you can use the ttf2pt1 program to convert the TrueType font to the pt1 font one by one (Detailed instructions are provided in the FPDF tutorial ). To support other chinese fonts, modify chinese. php as follows:

1: Replace the following line in the AddGBFont () method:

Function AddGBFont ($ family = 'GB', $ name = 'stsongstd-Light-Acro ')
{
$ Cw = $ GLOBALS ['GB _ widths'];
// $ Name = 'stsongstd-Light-Acro ';
$ CMap = 'gbkp-EUC-H ';
........

2: This is a Sample.

Require ('Chinese. php ');

$ Pdf = new PDF_Chinese ();
$ Pdf-> AddGBFont ('simsun', '');
$ Pdf-> AddGBFont ('simhei', 'simhei ');
$ Pdf-> AddGBFont ('simai', 'body_gb2312 ');
$ Pdf-> AddGBFont ('sinhang', '_ gb2312 '');
$ Pdf-> Open ();
$ Pdf-> AddPage ();
$ Pdf-> SetFont ('simsun', '', 20 );
$ Pdf-> Write (10, 'Simplified Chinese characters ');
$ Pdf-> SetFont ('simhei', '', 20 );
$ Pdf-> Write (10, 'Simplified Chinese characters ');
$ Pdf-> SetFont ('simai', '', 20 );
$ Pdf-> Write (10, 'Simplified Chinese characters ');
$ Pdf-> SetFont ('sinhang', '', 20 );
$ Pdf-> Write (10, 'Simplified Chinese characters ');
$ Pdf-> Output ();
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.