[英]How to use the printable module with a custom page in Drupal 8
我有一個帶有自定義路徑和自定義控制器的自定義頁面構建,我想使其可打印以下載它。
我正在查看可打印模塊,但我不知道如何獲取將其打印為 pdf 文件的路徑。
有什么想法嗎?
使用可打印模塊本身的控制器,我能夠生成 pdf。 您只需要將實體視圖(比較 showFormat 方法)與您自己的渲染數組(構建)進行交換。 在這個例子中實際上沒有使用 ConfigFactory。
<?php
namespace Drupal\your_module\Controller;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Controller\ControllerBase;
use Drupal\printable\PrintableFormatPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Returns response.
*/
class PrintViewController extends ControllerBase {
/**
* The printable format plugin manager.
*
* @var \Drupal\printable\PrintableFormatPluginManager
*/
protected $printableFormatManager;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* Constructs a PrintViewController object.
*
* @param \Drupal\printable\PrintableFormatPluginManager $printable_format_manager
* The printable format plugin manager.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory class instance.
*/
public function __construct(PrintableFormatPluginManager $printable_format_manager, ConfigFactory $config_factory) {
$this->printableFormatManager = $printable_format_manager;
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('printable.format_plugin_manager'),
$container->get('config.factory')
);
}
/**
* Builds the response.
*/
public function build() {
$build['content'] = [
'#type' => 'item',
'#markup' => $this->t('It works!'),
];
return $this->showFormat($build, 'pdf'); // One of 'pdf' / 'print'
}
/**
* Returns the entity rendered via the given printable format.
*
* @param array $build
* The render array be printed.
* @param string $printable_format
* The identifier of the hardcopy format plugin (i.e. print or pdf).
*
* @return \Symfony\Component\HttpFoundation\Response
* The printable response.
*/
public function showFormat(array $build, $printable_format) {
if (!$this->printableFormatManager->getDefinition($printable_format, FALSE)) {
throw new NotFoundHttpException();
}
$format = $this->printableFormatManager->createInstance($printable_format);
$format->setContent($build);
return $format->getResponse();
}
}
有幾種方法可以實現這一點:
使用實體打印模塊
實體打印允許您將任何 Drupal 實體(Drupal 7 和 8)或視圖(僅限 Drupal 8)打印為 PDF。
與打印模塊等其他模塊相比,該模塊重量輕,具有完整的測試覆蓋范圍,可用於 D7 和 D8 的生產。
使用新的打印模塊主機(可打印模塊)
這是打印模塊的新家。 該模塊使用 pdf_api 模塊來生成 PDF。
使用PrintFriendly 模塊(它支持本地化非常好)
Drupal 站點的 #1 打印和 PDF 按鈕。 無需編碼、css 或 print.css 的打印機友好頁面。 快速、簡單、專業。
Print Friendly & PDF 按鈕可在打印或創建 PDF 時節省紙張和墨水。 它快速、簡單且打印時看起來很棒。 現在添加按鈕,您的用戶將看到不同之處。
打印友好和 PDF 的工作原理 打印友好和 PDF 按鈕會自動創建頁面的打印機友好版本和 PDF 版本,而無需創建打印 CSS 文件。 無需編碼、黑客或編程。 只需安裝 Print Friendly & PDF 插件,激活並選擇完全自定義的設置。 它還使您的用戶能夠刪除圖像和文本段落,因此他們實際上只需要准確打印他們想要的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.