菜单

PHP 与pdf文档 与条码

2019年2月13日 - Php

http://www.fpdf.org/

必备的步调

https://github.com/Setasign/FPDF

1.导入require_once “tcpdf/tcpdf.php”; 工具源码在demo中

https://www.ntaso.com/fpdf-and-chinese-characters/

2.$pdf = new TCPDF(“P”, “mm”, “Magotan”, true, ‘UTF-8’,
false);//实例化3个pdf对象 mm表示坐标的单位,’Mondeo’表示纸张大小

<?php
 // 1.8.1
//define("FPDF_FONTPATH","font/");

try
{
require("../fpdf.php"); 
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial","B",16);
$pdf->Cell(40,10,"Hello World! 涂聚文"); //中文乱码
$pdf->Output();
}
catch(Exception $ex)
{
 echo($ex->getMessage());
}

?>

3.概念二个字体 $yahei = TCPDF_FONTS::addTTFfont(‘fonts/Microsoft
Yahei.ttf’, ‘TrueTypeUnicode’, ”, 96);//定义二个字体
字体文件存在项目fonts中

  

4.操作$pdf对象

https://www.phpbook.jp/fpdf/japan/index2.html

(1)新增一页  $pdf->AddPage();

缓解普通话标题:

(2)添加内容

http://www.fpdf.org/en/script/script92.php 

  文本:$pdf->Text($x, $y, “土豆刚”);//(x坐标,y坐标,突显内容)

把汉语后缀为.tff字体文件放入至:tfpdf\font\unifont
的文夹下即可。参考 http://www.fpdf.org/en/script/script92.php 

  图片:$pdf->Image(“log.png”, $x, $y);//图片路径,x坐标,y坐标

下载: http://www.fpdf.org/en/script/dl.php?id=92&f=zip

  条形码:$pdf->write1DBarcode(1234, ‘C128’, $x, $y, ”, ”, ”,”,
‘N’);//文本,‘c128’,x坐标,y坐标,宽度,中度,‘’,样式数组,‘’

https://dejavu-fonts.github.io/Download.html

5.概念2个pdf文档路径,将$pdf对象的情节与性情填写上去。

<?php

// Optionally define the filesystem path to your system fonts
// otherwise tFPDF will use [path to tFPDF]/font/unifont/ directory
// define("_SYSTEM_TTFONTS", "C:/Windows/Fonts/");  tFPDF
//http://www.fpdf.org/en/script/script92.php

require('tfpdf/tfpdf.php');

$pdf = new tFPDF();
$pdf->AddPage();

// Add a Unicode font (uses UTF-8)
//tfpdf\font\unifont 把中文的字体文件放入其内则可。
$pdf->AddFont('DejaVu','','3pdwg6wqe1jpcgh.ttf',true);   //msyh.ttf  设置字体即可,字体名称不可以定义为中文 简体篆体 :3pdwg6wqe1jpcgh.ttf
$pdf->SetFont('DejaVu','',14);

// Load a UTF-8 string from a file and print it
// 在操作系统下可以选择的字体可以显示,则也可以生成
$txt ="English: Hello World
Greek: Γειά σου κόσμος
Polish: Witaj świecie
Portuguese: Olá mundo
Russian: Здравствулте мир
Vietnamese: Xin chào thế giới,中国智造0123456789 涂聚文geovindu 生命的意义,涂聚文,中华人民共和国";// file_get_contents('HelloWorld.txt');
$pdf->Write(8,$txt);

// Select a standard font (uses windows-1252)
$pdf->SetFont('Arial','',14);
$pdf->Ln(10);
$pdf->Write(5,'The file size of this PDF is only 13 KB.');

$pdf->Output();
?>

$fn = “x.pdf”;//定义文档
$pdfcontent = $pdf->Output($fn, “S”);//将内容与性格放进输出流
$fh = fopen($fn, “w”);//打开输出流
fwrite($fh, $pdfcontent);//填写内容
fclose($fh);//关闭输出流

  图片 1

那时候二个pdf创建完结。

 

demo源码 http://pan.baidu.com/s/1pL31nqb

<?php

// Optionally define the filesystem path to your system fonts
// otherwise tFPDF will use [path to tFPDF]/font/unifont/ directory
// define("_SYSTEM_TTFONTS", "C:/Windows/Fonts/");  tFPDF
//http://www.fpdf.org/en/script/script92.php

require('tfpdf/tfpdf.php');

$pdf = new tFPDF();
$pdf->AddPage();

// Add a Unicode font (uses UTF-8)
//tfpdf\font\unifont 把中文的字体文件放入其内则可。
$pdf->AddFont('DejaVu','','3pdwg6wqe1jpcgh.ttf',true);   //msyh.ttf  设置字体即可,字体名称不可以定义为中文 简体篆体 :3pdwg6wqe1jpcgh.ttf
$pdf->SetFont('DejaVu','',14);

// Load a UTF-8 string from a file and print it
// 在操作系统下可以选择的字体可以显示,则也可以生成
$txt ="English: Hello World
Greek: Γειά σου κόσμος
Polish: Witaj świecie
Portuguese: Olá mundo
Russian: Здравствулте мир
Vietnamese: Xin chào thế giới,中国智造0123456789 涂聚文geovindu 生命的意义,涂聚文,中华人民共和国";// file_get_contents('HelloWorld.txt');
$pdf->Write(8,$txt);
$pdf->Ln(16);
$sheader=array("姓名","年龄","性别","工资","学历");
$data=array();
$data[0]=array("geovindu","1","男","100","");
$data[1]=array("sibodu","2","男","200","");
$data[2]=array("涂聚文","3","女","300","");
$data[3]=array("小涂","4","女","400","");
$data[4]=array("阿文","5","男","500","");
$data[5]=array("小文","6","女","600","");
$width=array(40,40,40,40); //设置每列宽度
for($i=0;$i<count($sheader);$i++)
{
 $pdf->Cell($width[$i],6,$sheader[$i],1);
}
$pdf->Ln(6);
foreach($data as $row)
{
 $pdf->Cell($width[0],6,$row[0],1);
 $pdf->Cell($width[1],6,$row[1],1);
 $pdf->Cell($width[2],6,$row[2],1);
 $pdf->Cell($width[3],6,$row[3],1);
 $pdf->Cell($width[4],6,$row[4],1);
 $pdf->Cell($width[5],6,$row[5],1);
 $pdf->Ln(6);
}
// Select a standard font (uses windows-1252) 
$pdf->SetFont('Arial','',14);
$pdf->Ln(10);
$pdf->Write(5,'The file size of this PDF is only 13 KB.');

$pdf->Output();
?>

技术总括:

  

1.第一说一下,pdf文档专擅认同的坐标系原点是
左上角,向下是Y轴的正级,向右是x轴的正极。假如你想直观的看出您可以那样:

<?php

// Optionally define the filesystem path to your system fonts
// otherwise tFPDF will use [path to tFPDF]/font/unifont/ directory
// define("_SYSTEM_TTFONTS", "C:/Windows/Fonts/");

require('tfpdf\tfpdf.php');

$pdf = new tFPDF();
$pdf->AddPage();

// Add a Unicode font (uses UTF-8)
$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->SetFont('DejaVu','',14);

// Load a UTF-8 string from a file and print it
$txt = file_get_contents('./tfpdf/HelloWorld.txt', FALSE, NULL, 20, 1400); //
$pdf->Write(8,$txt);

// Select a standard font (uses windows-1252)
$pdf->SetFont('Arial','',14);
$pdf->Ln(10);
$pdf->Write(5,'The file size of this PDF is only 13 KB.');

$pdf->Output();
?>
    $pdf->SetFont($yahei, 'B', 5, '', true);
    for($x = 0;$x < 200;$x=$x+5)
    {
         $pdf->Text($x, 0, $x."-");

    }

    for($y = 0;$y < 260;$y=$y+5)
    {
        $pdf->Text(0, $y, $y);

    }

  

展现结果:

 图片 2

当!当!当!当!这样就直观了,然后须求把成分放到什么岗位就那啥了撒。

2.然后免不了你会想旋转一下 文字、图片什么滴。

这边很奇怪,笔者觉着是就偏偏的团团转指定的因素,结果吗,一旋转整个坐标系都旋转了。Rorate方法里面(“旋转角度”,‘X’,‘Y’)那里的XY应该是旋转之后的坐标系原点在原坐标系中的坐标,可是如同不是很纯粹,感觉单位变了同一。

 

    $pdf->StartTransform();
    $pdf->Rotate(-30,0,60);//表示整个坐标系以原来的坐标0,60坐标(感觉又不是。。)顺时针旋转  在$pdf->StartTransform()与$pdf->StopTransform()之间就使用旋转后的坐标

    for($x = 0;$x < 200;$x=$x+5)
    {
        $pdf->Text($x, 0, $x.'-');

    }

    for($y = 0;$y < 260;$y=$y+5)
    {
        $pdf->Text(0, $y, $y);

    }
  $pdf->StopTransform();

来得结果:

图片 3

 

下一场实际举多少个例证:

$pdf->write1DBarcode("哎木土豆刚", 'C128', 10, 10, '', '', '',array("text"=>true,"font"=>$yahei,"fontsize"=>10), 'N');

来得结果:

图片 4

旋转:

    //旋转文字(应该叫旋转坐标系更准确)
    $pdf->StartTransform();
    $pdf->Rotate(-30,0,60);//表示整个坐标系以原来的坐标0,60坐标(感觉又不是。。)顺时针旋转  在$pdf->StartTransform()与$pdf->StopTransform()之间就使用旋转后的坐标

    for($x = 0;$x < 200;$x=$x+5)
    {
        $pdf->Text($x, 0, $x.'-');

    }

    for($y = 0;$y < 260;$y=$y+5)
    {
        $pdf->Text(0, $y, $y);

    }

    $pdf->write1DBarcode("旋转土豆刚", 'C128', 10, 10, '', '', '',array("text"=>true,"font"=>$yahei,"fontsize"=>10), 'N');

    $pdf->StopTransform();

图片 5

图片 6图片 7

<?php 

    require_once "tcpdf/tcpdf.php";
    $pdf = new TCPDF("P", "mm", "A4", true, 'UTF-8', false);
    $pdf->SetCreator("potatog");
    $pdf->SetAuthor('potatog');
    $pdf->SetTitle("potatog");
    $pdf->SetSubject("potatog");

    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->setFontSubsetting(true);

    $yahei = TCPDF_FONTS::addTTFfont('fonts/Microsoft Yahei.ttf', 'TrueTypeUnicode', '', 96);

    $pdf->AddPage();


    $pdf->SetFont($yahei, 'B', 5, '', true);
    for($x = 0;$x < 200;$x=$x+5)
    {
         $pdf->Text($x, 0, $x."-");

    }

    for($y = 0;$y < 260;$y=$y+5)
    {
        $pdf->Text(0, $y, $y);

    }

//     $pdf->write1DBarcode("哎木土豆刚", 'C128', 10, 10, '', '', '',array("text"=>true,"font"=>$yahei,"fontsize"=>10), 'N');

    //旋转文字(应该叫旋转坐标系更准确)
    $pdf->StartTransform();
    $pdf->Rotate(-30,0,60);//表示整个坐标系以原来的坐标0,60坐标(感觉又不是。。)顺时针旋转  在$pdf->StartTransform()与$pdf->StopTransform()之间就使用旋转后的坐标

    for($x = 0;$x < 200;$x=$x+5)
    {
        $pdf->Text($x, 0, $x.'-');

    }

    for($y = 0;$y < 260;$y=$y+5)
    {
        $pdf->Text(0, $y, $y);

    }

    $pdf->write1DBarcode("旋转土豆刚", 'C128', 10, 10, '', '', '',array("text"=>true,"font"=>$yahei,"fontsize"=>10), 'N');

    $pdf->StopTransform();


//     $pdf->Image("log.png", $x+5, $y+50);

//     $pdf->write1DBarcode(1234, 'C39', $x, $y+20, '', '', '',array("text"=>true,"label"=>"1234","font"=>$yahei,"fontsize"=>10), 'N');//这个C39用浏览器扫描不出来  C128才可以
//     $pdf->write1DBarcode(1234, 'C128', $x, $y+20, '', '', '',array("text"=>true,"label"=>"1234","font"=>$yahei,"fontsize"=>5,'border'=>true), 'N');
//     $pdf->write1DBarcode(1234, 'C128', $x+50, $y+20, '', '', '',array("text"=>true,"font"=>$yahei,'stretchtext'=>0,"fontsize"=>5), 'N');

//     $pdf->write1DBarcode(12345, 'C128', '', '', '80', '30', '',array("text"=>true,"font"=>$yahei,"fontsize"=>10,'border'=>true,'stretchtext'=>0,'stretch'=>'x'), 'N');



    $fn = "x.pdf";
    $pdfcontent = $pdf->Output($fn, "S");
    $fh = fopen($fn, "w");
    fwrite($fh, $pdfcontent);
    fclose($fh);
    echo "<a href='".$fn."'>点我</a>"
?>

修改后的index.php

 

首回写发现本身还不是很会表明,干脆源码给出去,直接用。

再就是本人也不是很懂,继续努力。

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图