简体   繁体   English

TYPO3扩展:生成PDF

[英]TYPO3 Extension: Generate a PDF

Im trying to make an extension with Kickstarter that overrides the normal rendering of the page, and renders a PDF file. 我试图用Kickstarter进行扩展,覆盖页面的正常渲染,并呈现PDF文件。 For this im using FPDF. 为此我使用FPDF。 But im not sure how to do it. 但我不知道该怎么做。 I tried doing this, but it didnt work: 我试过这样做,但它没有用:

<?php

// require_once(PATH_tslib . 'class.tslib_pibase.php');

class tx_ishurkunde_pi1 extends tslib_pibase {
    public $prefixId      = 'tx_ishurkunde_pi1';
    public $scriptRelPath = 'pi1/class.tx_ishurkunde_pi1.php';
    public $extKey        = 'ish_urkunde';
    public $pi_checkCHash = TRUE;

    public function main($content, array $conf) {

        if (!t3lib_extMgm::isLoaded('fpdf')) return "Error!";

        $pdf = new FPDF();
        $pdf->AddPage();
        $content = $pdf->Output('', 'S');
        return $content;

    }
}
?>

It still keeps rendering the normal web template. 它仍然保持渲染正常的Web模板。 What am I missing? 我错过了什么?

FYI, Im not trying to render the HTML as PDF. 仅供参考,我不是要将HTML呈现为PDF。 Im trying to generate a PDF from scratch, using the URL parameters are text variables. 我试图从头开始生成PDF,使用URL参数是文本变量。

As far as I understood, your aim is to render a PDF instead of page elements. 据我所知,您的目标是呈现PDF而不是页面元素。

Your current approach will not work since you are inserting a plugin onto the page. 由于您要在页面上插入插件,因此您当前的方法无效。 The plugin's return value is then given back to the TYPO3 content parser, and if the page has finished parsing, it is displayed. 然后将插件的返回值返回给TYPO3内容解析器,如果页面已完成解析,则显示该插件。 There is no part in it where you can throw over the whole page rendering; 它没有任何部分可以抛弃整个页面渲染; At least it is not intended to do, and you shouldn't to (albeit there are extensions that do this). 至少它不打算这样做,你不应该这样做(虽然有扩展可以做到这一点)。

The eID approach would be to either create an eID script (have a look at dd_googlesitemap) which is called via GET param and renders only what you need. eID方法是创建一个eID脚本(看看dd_googlesitemap),它通过GET参数调用,只呈现你需要的东西。 There you basically can do everything you want to. 你基本上可以做你想做的一切。

In your extension's ext_localconf.php you register the eID script, like this: 在您的扩展的ext_localconf.php您注册了eID脚本,如下所示:

$GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$_EXTKEY] = "EXT:{$_EXTKEY}/path/to/my/EIDHandler.php";

An Example eID handler structure: 示例eID处理程序结构:

class Tx_MyExt_EIDHandler
{
  public function main()
  {
    // Your code here
  }
}

$output = t3lib_div::makeInstance('Tx_MyExt_EIDHandler');
$output->main();

To call your eID script in the frontend, you append the appropriate GET params, like http://example.com/index.php?eID=tx_myext . 要在前端调用您的eID脚本,请附加相应的GET参数,例如http://example.com/index.php?eID=tx_myext This is the array key you defined in your ext_localconf.php (in my example, it is $_EXTKEY , but it can basically be any string). 这是你在ext_localconf.php定义的数组键(在我的例子中,它是$_EXTKEY ,但它基本上可以是任何字符串)。

The plugin/typoscript approach would be like eg TemplaVoila does it: You create a PAGE type and call a user_func which does your things. 插件/ typoscript方法就像例如TemplaVoila那样:你创建一个PAGE类型并调用user_func来完成你的工作。 This would be the fastest approach because you already have a plugin. 这是最快的方法,因为你已经有了一个插件。 Important is that you render your own page type with only your plugin in it. 重要的是,您只需使用插件即可呈现自己的页面类型。

Example TypoScript: TypoScript示例:

specialPage = PAGE
specialPage {
  type = 2
  10 = USER
  10.userFunc = tx_myextension_pi1->myFunc
}

After that, you can call your new page with http://example.com/index.php?type=2 . 之后,您可以使用http://example.com/index.php?type=2调用新页面。 However, headers etc are still rendered and you may need to remove them. 但是,仍然会呈现标题等,您可能需要删除它们。

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

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