php TCPDF:如何以正确的方式设置字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29795896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:35:30  来源:igfitidea点击:

TCPDF: How to set FONT SIZE in right way

phppdffont-sizetcpdfdimensions

提问by Patrik

I want to set some text-blocks by TCPDF. But I have some problems with font size. First text block is on x-y/5-5, and his font size is 5 to. But it is samaller then 5. Font-size in TCPDF is not in the same units like other dimensions ?

我想通过 TCPDF 设置一些文本块。但是我对字体大小有一些问题。第一个文本块在 xy/5-5 上,他的字体大小是 5 to。但它比其他尺寸小 5. TCPDF 中的字体大小与其他尺寸的单位不同?

TCPDF - dimensions and font-size

TCPDF - 尺寸和字体大小

PHP

PHP

$text1 = 'AAAg';
$text1_x = 5;
$text1_y = 5;
$text1_font_size = 5;

$text2 = 'BBBg';
$text2_x = 10;
$text2_y = 10;
$text2_font_size = 10;

$text3 = 'CCCg';
$text3_x = 15;
$text3_y = 15;
$text3_font_size = 15;
// I tried  $pdf->Cell and $pdf->Text... both are doing the same...

Web example.

网络示例。

回答by Patrik

OK I found answer and solution. When we create new PDF document in tcPDF, dimensions units whole document can be in formats like mm, cm, pt, px. But fonts are in points - pt.

好的,我找到了答案和解决方案。当我们在 tcPDF 中创建新的 PDF 文档时,整个文档的尺寸单位可以是 mm、cm、pt、px 等格式。但是字体以点为单位 - pt。

So solution...

所以解决...

  1. Set the documents units with 'setPageUnit'.
  2. If we have dimensions in pixels, we must convert it by 'pixelsToUnits'.
  1. 使用“ setPageUnit”设置文档单位。
  2. 如果我们有以像素为单位的尺寸,我们必须将其转换为“ pixelsToUnits”。

PHP - tcPDF Exampe:

PHP-tcPDF 示例

$pdf->setPageUnit('pt');
$document_width = $pdf->pixelsToUnits('100');
$document_height = $pdf->pixelsToUnits('100');
$x = $pdf->pixelsToUnits('20');
$y = $pdf->pixelsToUnits('20');
$font_size = $pdf->pixelsToUnits('20');
$txt = 'AAAg';

$pdf->SetFont ('helvetica', '', $font_size , '', 'default', true );
$pdf->Text  ( $x, $y, $txt, false, false, true, 0, 0, '', false, '', 0, false, 'T', 'M', false );

回答by Selva kumar

Changing font size in TCPDF ... Which can set using below code :

更改 TCPDF 中的字体大小...可以使用以下代码进行设置:

$pdf = new TCPDF();
$pdf->SetFont('Font family', '', font size here);

Which are default setting in TCPDF

哪些是 TCPDF 中的默认设置