简体   繁体   English

如何在 TCPDF 中省略 Helvetica?

[英]How can I omit Helvetica with TCPDF?

I'm making a PDF with TCPDF, and I'm trying to make the file as small as possible.我正在使用 TCPDF 制作 PDF,并且我正在尝试使文件尽可能小。 The font I'm using is Open Sans.我使用的字体是 Open Sans。 I'm not (intentionally, at least) using Helvetica anywhere in the PDF.我没有(至少有意地)在 PDF 的任何地方使用 Helvetica。 When I view the included fonts with Adobe Reader in my outputted PDF file, both Open Sans and Helvetica are listed.当我在输出的 PDF 文件中使用 Adob​​e Reader 查看包含的字体时,会列出 Open Sans 和 Helvetica。 I have noticed that if I AddFont() other fonts, the outputted PDF gets bigger.我注意到如果我AddFont()其他字体,输出的 PDF 会变大。

To save space, how can I tell TCPDF to not include Helvetica in the file?为了节省空间,我如何告诉 TCPDF 在文件中不包含 Helvetica?

The Helvetica font is added by TCPDF for two reasons:由于两个原因,TCPDF 添加了 Helvetica 字体:

  1. On initialization the TCPDF class sets the default font to Helvetica (in the constructor) and therefore adds this font to the fonts list of the document.在初始化时,TCPDF 类将默认字体设置为 Helvetica(在构造函数中),因此将此字体添加到文档的字体列表中。

For older versions: To prevent this, you can edit the file config/tcpdf_config.php and change the constant PDF_FONT_NAME_MAIN to your desired default font name (should be around line 155).对于旧版本:为了防止这种情况,您可以编辑文件config/tcpdf_config.php并将常量PDF_FONT_NAME_MAIN更改为您想要的默认字体名称(应该在第 155 行左右)。 Note that you must not use any core font because they will never be embedded.请注意,您不得使用任何核心字体,因为它们永远不会被嵌入。

For newer versions: Define PDF_FONT_NAME_MAIN with your desired default font name before including the TCPDF files.对于较新版本:包含 TCPDF 文件之前,使用所需的默认字体名称定义PDF_FONT_NAME_MAIN Example:例子:

define('PDF_FONT_NAME_MAIN', 'freesans');
include_once 'path/to/tcpdf.php';
  1. TCPDF adds an invisible link "Powered by www.tcpdf.org" at the bottom of the page. TCPDF 在页面底部添加了一个隐形链接“Powered by www.tcpdf.org”。

To prevent this you have to use an override class like this:为了防止这种情况,您必须使用这样的覆盖类:

class MyPdf extends TCPDF {

    public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
        // call parent constructor
        parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
        // disable the tcpdf link
        $this->setTcpdfLink(false);
    }

    /**
     * Allows to disable the invisible "Powered by www.tcpdf.org" link at the bottom of the page.
     * @param type $tcpdflink
     */
    public function setTcpdfLink($tcpdflink = true) {
        $this->tcpdflink = $tcpdflink ? true : false;
    }

}

The Helvetica font is one of the standard 14 core PDF fonts, so it is not embedded in the PDF when it is used. Helvetica 字体是标准的 14 种核心 PDF 字体之一,因此在使用时不会嵌入到 PDF 中。 If you look in the TCPDF fonts directory you will notice that the Helvetica file only contains a description of the font and not a copy of the font.如果您查看 TCPDF 字体目录,您会注意到 Helvetica 文件仅包含字体描述,而不包含字体副本。 Therefore it shouldn't be significantly increasing the file size.因此,它不应该显着增加文件大小。

Solution解决方案

The Helvetica font is set as the default font in the TCPDF config files. Helvetica 字体被设置为 TCPDF 配置文件中的默认字体。 From my testing, it appears that this causes it to be set as a font in the generated PDF files even when it is not used.从我的测试来看,这似乎导致它被设置为生成的 PDF 文件中的字体,即使它没有被使用。 Changing the default fonts in your TCPDF configuration files should prevent this from happening.更改 TCPDF 配置文件中的默认字体应该可以防止这种情况发生。

I have to face the same issue.我必须面对同样的问题。 I have tried the JOR solution.我已经尝试过JOR解决方案。 its correct but it still shows the Helvetica font family in my pdf.它是正确的,但它仍然在我的 pdf 中显示 Helvetica 字体系列。

for my pdf, I am using SVG image.so it displays the Helvetica .对于我的 pdf,我使用的是 SVG image.so 它显示 Helvetica 。 in the tcpdf.php protected property called $svgstyles has the SVG font family as Helvetica.在名为 $svgstyles 的 tcpdf.php 受保护属性中,SVG 字体系列为 Helvetica。

Just find $tcpdflink in tcpdf.php and make that variable false.只需在 tcpdf.php 中找到 $tcpdflink 并将该变量设为 false。 This works for me这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM