此处将为大家介绍关于当使用特殊(slovic)字符đ时,PHP的TCPDF创建空PDF的详细内容,此外,我们还将为您介绍关于LARAVEL使用TCPDF生成PDF文档,html转成pdf-TCPDF、
此处将为大家介绍关于当使用特殊(slovic)字符đ时,PHP的TCPDF创建空PDF的详细内容,此外,我们还将为您介绍关于LARAVEL 使用TCPDF生成PDF文档,html转成pdf - TCPDF、php – TCPDF – 如何在使用tcpdf创建的pdf中显示具有绝对位置的div?、php – TCPDF/FPDF – 分页符问题、php – 使用AJAX和Jquery使用FPDF创建pdf的有用信息。
本文目录一览:- 当使用特殊(slovic)字符đ时,PHP的TCPDF创建空PDF
- LARAVEL 使用TCPDF生成PDF文档,html转成pdf - TCPDF
- php – TCPDF – 如何在使用tcpdf创建的pdf中显示具有绝对位置的div?
- php – TCPDF/FPDF – 分页符问题
- php – 使用AJAX和Jquery使用FPDF创建pdf
当使用特殊(slovic)字符đ时,PHP的TCPDF创建空PDF
数据是用户生成的,所以我不知道他们是否计划使用这些类型的字符 – 这就是为什么我认为我应该使用UTF-8.但显然这是不对的.
PDF已创建,但它完全为空/白色.
这是我的相关代码:
$pdf = new XTCPDF('P',PDF_UNIT,'Letter',true,'UTF-8',false); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); $pdf->SetMargins($pageMargin,$pageMargin,$pageMargin); //PDF_MARGIN_LEFT,PDF_MARGIN_TOP,PDF_MARGIN_RIGHT $pdf->SetAutopageBreak(TRUE,$pageMargin/2); $pdf->setimageScale(PDF_IMAGE_SCALE_RATIO); $pdf->SetFont('helvetica','',10,true); $pdf->AddPage(); $pdf->setJPEGQuality(70);
我找到了this answer但是换到这个也不起作用:
$pdf = new XTCPDF('P',false,'ISO-8859-1',false);
我的数据片段(用户名,地址等)目前包含如下:
trim(mb_convert_encoding($myHtml,"HTML-ENTITIES","UTF-8"))
问题:
我可以改变什么来做到这一点:
>我可以创建一个像“đ”这样的字符的PDF
>我可以进行设置,因此无论用户使用哪个字符,它都会创建.
解决方法
基本测试(注意我将此PHP脚本保存为UTF8文件):
require './tcpdf/tcpdf.PHP'; function write($pdf) { $pdf->Write(0,'test đ ',0); $pdf->writeHTML(trim(mb_convert_encoding('test2 đ',"UTF-8")),''); } $pdf = new TCPDF('P',false); $pdf->AddPage(); $pdf->SetFont('helvetica',20); write($pdf); $pdf->SetFont('freesans',20); write($pdf); $pdf->Output();
这给了我以下PDF输出(在Firefox中呈现):
查看TCPDF example for including an external UTF-8 text file,您可以看到以下非常有趣的内容:
$pdf->SetFont('freeserif',12);
奇怪的是,freeserif并没有被列为standard TCPDF fonts之一,而是在与TCPDF一起分发的fonts文件夹中(在这个文件夹中也看到了freesans,所以我在上面的例子中使用了它).我只能假设文档不是最新的.
您似乎可以选择使用freesans或通过AddFont()方法加载第三方字体.
编辑:我测试了上面的一个稍微旧的TCPDF版本:6.0.099.
LARAVEL 使用TCPDF生成PDF文档,html转成pdf - TCPDF
上次,简单的提了使用PHPWord生成Word文档,这次,就简单的讲下tcpdf生成PDF文档。效果图如下:
首先,引入TCPDF类库,使用COMPOSER
composer require tecnickcom/tcpdf
代码示例 :
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// 设置文档信息 $pdf->SetCreator('Hello world'); $pdf->SetAuthor('dyk'); $pdf->SetTitle('TCPDF示例'); $pdf->SetSubject('TCPDF示例'); $pdf->SetKeywords('TCPDF, PDF, PHP'); // 设置页眉和页脚信息 $pdf->SetHeaderData('tcpdf_logo.jpg', 30, 'www.marchsoft.cn', '三月软件!', [0, 64, 255], [0, 64, 128]); $pdf->setFooterData([0, 64, 0], [0, 64, 128]); // 设置页眉和页脚字体 $pdf->setHeaderFont(['stsongstdlight', '', '10']); $pdf->setFooterFont(['helvetica', '', '8']); // 设置默认等宽字体 $pdf->SetDefaultMonospacedFont('courier'); // 设置间距 $pdf->SetMargins(15, 15, 15);//页面间隔 $pdf->SetHeaderMargin(5);//页眉top间隔 $pdf->SetFooterMargin(10);//页脚bottom间隔 // 设置分页 $pdf->SetAutopageBreak(true, 25); // 设置自动换页 $pdf->SetAutopageBreak(TRUE, PDF_MARGIN_BottOM); // 设置图像比例因子 $pdf->setimageScale(1.25); // 设置默认字体构造子集模式 $pdf->setFontSubsetting(true); // 设置字体 stsongstdlight支持中文 $pdf->SetFont('stsongstdlight', '', 14); // 添加一页 $pdf->AddPage(); $pdf->Ln(5);//换行符 $html = ' <table width="400" border="1"> <tr> <th align="left">消费项目</th> <th align="right">一月</th> <th align="right">二月</th> </tr> <tr> <td align="left">衣服</td> <td align="right">$241.10</td> <td align="right">$50.20</td> </tr> <tr> <td align="left">化妆品</td> <td align="right">$30.00</td> <td align="right">$44.45</td> </tr> <tr> <td align="left">食物</td> <td align="right">$730.40</td> <td align="right">$650.00</td> </tr> <tr> <th align="left">总计</th> <th align="right">$1001.50</th> <th align="right">$744.65</th> </tr> </table> '; $pdf->writeHTML($html, true, false, true, false, ''); //输出PDF $pdf->Output('t.pdf', 'I');//I输出、D下载
官方文档上有示例,附:https://tcpdf.org/examples/
官方文档:https://tcpdf.org/
有兴趣的话,可以深入的了解一下
php – TCPDF – 如何在使用tcpdf创建的pdf中显示具有绝对位置的div?
我有以下tcpdf代码:
<?PHP
require_once('config/lang/eng.PHP');
require_once('tcpdf.PHP');
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {
//Page header
public function Header() {
}
// Page footer
public function Footer() {
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Stimart');
$pdf->SetTitle('test');
$pdf->SetSubject('test');
$pdf->SetKeywords('test');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_logo, PDF_HEADER_logo_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(40, 40, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutopageBreak(TRUE, PDF_MARGIN_BottOM);
//set image scale factor
$pdf->setimageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('times', '', 12);
// add a page
$pdf->AddPage();
// set some text to print
$html = '<div>Hi, I am an ABSOLUTE div.</div>';
$pdf->writeHTML($html, true, false, false, false, '');
// ---------------------------------------------------------
$pdf->lastPage();
//Close and output PDF document
$pdf->Output('test.pdf', 'I');
//============================================================+
// END OF FILE
//============================================================+
在“//设置一些要打印的文本”之后,我设置了要显示的HTML代码,但它没有按预期工作.
在这种情况下,我需要传递一个具有绝对位置的div,但是当代码运行时,它会创建显示相对位置div的pdf页面.
我该如何解决这个恼人的问题?
解决方法:
TCPDF的CSS支持非常有限.它不支持所有属性.
目前,仅支持以下CSS属性:
> font-family
> font-size
> font-weight
> font-style
>颜色
>背景色
>文字装饰
>宽度
>身高
> text-align
所以尝试删除其他属性,并告诉它是否有效.
php – TCPDF/FPDF – 分页符问题
我试图用TCPDF做同样的事情,但每次在页面断点点附近添加一个新单元时,页面中断仍然是同样的问题…
例:
http://www.online-økonomi.dk/_tst_fpdf.php
require_once '../class/download/fpdf/fpdf.PHP'; class File_PDF { private $pdf; private $col_product = 25; private $col_unit = 12; private $col_price = 20; private $col_count = 14; private $col_discount = 12; private $col_vat = 12; private $col_sum = 22; private $width = 200; private $line_height = 4.2; private $margin_top = 30; public function generate(){ $this->pdf = new FPDF(); $this->pdf->AddPage(); $this->pdf->SetdisplayMode('real'); $this->pdf->SetAutopageBreak(true,150); if($this->products){ $i = 0; $this->color_light(); foreach($this->products as $product){ $this->add_product($product,$i % 2 ? true:false); $i++; } } $this->pdf->Output(); } private function add_product($product,$fill){ $this->txt(); $x = $this->width; $y = $this->pdf->GetY(); $this->cell_sum($this->col_sum,$x,$y,$product['sum'] / 100,'R',$fill); $this->cell_vat($this->col_vat,$product['vat_percent'],$fill); $this->cell_discount($this->col_discount,$product['discount_percent'] / 100,$fill); $this->cell_count($this->col_count,$product['count'] / 100,$fill); $this->cell_price($this->col_price,$product['price'] / 100,$fill); $this->cell_unit($this->col_unit,$product['unit_name'],'L',$fill); $this->cell_name(0,$product['name'],$fill); $this->cell_product($this->col_product,$product['product_id_'],$fill); } private function cell_sum($width,&$x,$str,$align,$fill=false){ $this->cnstr_cell($width,$fill); } private function cell_vat($width,$fill); } private function cell_discount($width,$fill); } private function cell_count($width,$fill); } private function cell_price($width,$fill); } private function cell_unit($width,$fill); } private function cell_name($width,$fill=false){ $this->pdf->SetXY($this->col_product + 10,$y); $this->pdf->MultiCell($x - $this->col_product - 10,$this->line_height,$fill); } private function cell_product($width,$fill=false){ $this->pdf->SetXY(10,$y); $this->pdf->MultiCell($this->col_product,$fill); } private function cnstr_cell($width,$align='L',$fill=false){ $x -= $width; $this->pdf->SetXY($x,$y); $this->pdf->MultiCell($width,$fill); } private function color_light(){ $this->pdf->SetFillColor(200,200,200); } private function txt(){ $this->pdf->SetFont('Arial','',8.5); } private function txt_marked(){ $this->pdf->SetFont('Arial','B',8.5); } private $products = array( array( 'product_id_' => 'ADS1550','name' => 'name','unit_name' => 'pcs','price' => 182450000,'count' => 310000,'discount_percent' => 19900,'vat_percent' => 0,'sum' => 1587057200 ),array( 'product_id_' => 'ADS1550','sum' => 1587057200 ) ); } $PDF = new File_PDF(); $PDF->generate();
默认页面高度似乎是297,SetAutopageBreak()从中减去150.所以当Y cell_height比147更重要时,你总是在调用cnstr_cell()时获得一个新页面.
要防止这种情况,您需要自己调用AddPage().在add_product()方法中添加此检查:
$x = $this->width; $y = $this->pdf->GetY(); if (($y + $this->line_height) >= 147) { $this->pdf->AddPage(); $y = 0; // should be your top margin }
顺便说一句.我最近还必须生成动态PDF,最后使用wkhtmltopdf,它比所有PHP库更容易使用和自定义.我建议看看它.
php – 使用AJAX和Jquery使用FPDF创建pdf
我现在拥有的是一个HTML页面,其中包含各种数据行和旁边的打印按钮.当有人点击Print时,我通过调用AJAX来使用Jquery发送相应的数据.
这是我的JQUERY代码:
$('.printbtn').live('click',function(){ var printdata = 'name=' + name_t + '&address=' + address_t; $.post('print.PHP',printdata,function(){ }); return false; });
这是print.PHP
$name = $_POST['name']; $address = $_POST['address']; require ("fpdf17/fpdf.PHP"); $pdf = new FPDF('P','mm',array(90,100)); $pdf->AddPage(); $pdf->SetFont('Arial','B',12); $pdf->Cell(0,10,'name: '.$name); $pdf->Cell(0,'address: '.$address); $pdf->Output(); ?>
但我不会得到PDF作为回报.怎么了??事实上,没有发生..
我想要检索pdf文件并将其发送到打印
>使用一些PDF javascript库直接渲染PDF
没有插件的javascript,如mozilla pdf.js或jspdf.
并尝试查看这些库是否允许您直接设置pdf
二进制数据(你在ajax中收到的).
>另一种选择是接收pdf数据作为base64编码和使用
a Data URI将window.location.href设置为该数据uri,
虽然在这种情况下,PDF可能会作为下载提供
对话框而不是直接在页面中加载,你必须测试
那.对于IE 8中的pdf,数据uri支持也非常有限
较旧的,请参阅维基百科data uri page了解更多详情.
>另请参阅this answer,了解如何加载pdf是不可能的直接从ajax和你有什么其他选择尝试做什么你想要(主要是将pdf保存为服务器上的临时文件并使用它使用window.location.href或< iframe>或者window.open)
我们今天的关于当使用特殊(slovic)字符đ时,PHP的TCPDF创建空PDF的分享已经告一段落,感谢您的关注,如果您想了解更多关于LARAVEL 使用TCPDF生成PDF文档,html转成pdf - TCPDF、php – TCPDF – 如何在使用tcpdf创建的pdf中显示具有绝对位置的div?、php – TCPDF/FPDF – 分页符问题、php – 使用AJAX和Jquery使用FPDF创建pdf的相关信息,请在本站查询。
本文标签: