如何提高从PDF生成的图像的质量?

问题描述 投票:1回答:1

我们使用TYPO3 9.5.13GraphicsMagick 1.3.29Ghostscript 9.27BK2K\\BootstrapPackage 11.0.1

将PDF用作普通图像没有问题。但是现在我想以全列宽(〜1000像素)对PDF进行“预览”。而且,尽管PDF具有高分辨率,但是生成的图像的宽度仅为595px,几乎所有文本都不可读。

[Image-CE出现问题,就像我要增强的上载CE:每次我要使用全列宽的图像时,它都会以较差的分辨率呈现,并且图像看起来会失真。

这里是生成图像的一小部分:enter image description here

以及与PDF阅读器中显示的PDF相同的区域:enter image description here

流体部分:

<img loading="lazy" 
     src="{f:uri.image(image: file, cropVariant: 'default', maxWidth: 1100)}" 
     width="{bk2k:lastImageInfo(property: 'width')}" 
     height="{bk2k:lastImageInfo(property: 'height')}" 
     intrinsicsize="{bk2k:lastImageInfo(property: 'width')}x{bk2k:lastImageInfo(property: 'height')}" 
     title="{file.properties.title}" 
     alt="{file.properties.alternative}">

其结果类似于:

<img loading="lazy" 
     src="/fileadmin/_processed_/3/2/csm_Warum_D-Arzt_6afd8ad8d4.png"
     intrinsicsize="595x842" 
     title="" 
     alt="" 
     width="595" 
     height="842">

编辑:

如果使用此FLUID:

<img loading="lazy" 
     src="{f:uri.image(image: file, cropVariant: 'default', width: 1100)}" 
     width="{bk2k:lastImageInfo(property: 'width')}" 
     height="{bk2k:lastImageInfo(property: 'height')}" 
     intrinsicsize="{bk2k:lastImageInfo(property: 'width')}x{bk2k:lastImageInfo(property: 'height')}" 
     title="{file.properties.title}" 
     alt="{file.properties.alternative}">

我得到:

<img loading="lazy" 
     src="/fileadmin/_processed_/3/2/csm_Warum_D-Arzt_2ffb63b15f.png" 
     intrinsicsize="1100x1557" 
     title="" 
     alt="" 
     width="1100" 
     height="1557">

图像较大(并溢出容器),但质量同样较差,请注意较大的像素:

enter image description here

pdf typo3 ghostscript graphicsmagick typo3-9.x
1个回答
0
投票

实际上,PDF是NOT图像。它是一种容器格式,可以包含具有不同色彩空间和尺寸的矢量和图像。位图图像具有固定的尺寸宽度,高度,密度,而PDF则没有。最初,它是经过创建和优化的,适用于打印机,而不适用于屏幕。

TYPO3通过后端中的一条消息反映出这一点:enter image description here

恕我直言,没有一种完美的方法来处理PDF使其表现得像图像,正如您所知道的那样,它的输出格式不是输入格式(正确)。获得可接受结果的两种方法:

  1. 扩展内容元素或创建新元素,并为PDF预览图像添加第二个图像插槽。使用图形程序自己创建预览图像。
  2. 编写您自己的viewhelper并创建自己的缩略图

解决方案1将为编辑人员带来更多工作。对我来说不是最佳实践。


我会使用自己的viewhelper。

为PDF添加您自己的渲染类型:

<f:switch expression="{file.type}">
  <f:case value="5">
    <f:render partial="Media/Type/Pdf" arguments="{file: file, dimensions: dimensions, data: data, settings: settings}" />
  </f:case>
  <f:defaultCase>
    <f:render partial="Media/Type/Image" arguments="{file: file, dimensions: dimensions, data: data, settings: settings}" />
  </f:defaultCase>
</f:switch>

部分媒体/类型/ Pdf

{namespace cv=Conversion\HelperUtils\ViewHelpers}
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" xmlns:ce="http://typo3.org/ns/TYPO3/CMS/FluidStyledContent/ViewHelpers" data-namespace-typo3-fluid="true">
  <cv:forEachPdfThumbnail document="{file}" pages="1" as="pdfPreviewPage">
    <f:image src="{pdfPreviewPage}" alt="" />
  </cv:forEachPdfThumbnail>
</html>

ViewHelper:

此viewhelper使用CommandUtility :: imageMagickCommand从PDF转换多个页面。您可以将密度提高到更高的值以提高质量。如前所述,该viewhelper是几年前开发的,可以进行改进(例如,保存到fileadmin / processed而不是typo3temp。请随时克隆和改进:https://github.com/conversion1/t3-pdfthumbnailviewhelper/blob/master/ForEachPdfThumbnailViewHelper.php

public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{

    $templateVariableContainer = $renderingContext->getVariableProvider();

    /** @var \TYPO3\CMS\Core\Resource\FileReference $document */
    $document = $arguments['document'];
    $pages = explode(',', str_replace(' ', '', $arguments['pages']));

    $colorspace = TRUE === isset($GLOBALS['TYPO3_CONF_VARS']['GFX']['colorspace']) ? $GLOBALS['TYPO3_CONF_VARS']['GFX']['colorspace'] : 'RGB';
    $absFilePath = GeneralUtility::getFileAbsFileName($document->getOriginalFile()->getPublicUrl());
    $destinationPath = 'typo3temp/';
    $destinationFilePrefix = 'pdf-prev_' . $document->getOriginalFile()->getNameWithoutExtension();
    $destinationFileExtension = 'png';

    $output = '';

    foreach ($pages as $pageNumber) {

        if($pageNumber > 0) {
            $pageNumber = intval($pageNumber);
        } else {
            $pageNumber = 1;
        }

        $destinationFileSuffix =  '_page-' . $pageNumber;
        $absDestinationFilePath = GeneralUtility::getFileAbsFileName($destinationPath . $destinationFilePrefix . $destinationFileSuffix . '.' . $destinationFileExtension);

        $imgArguments = '-colorspace ' . $colorspace;
        $imgArguments .= ' -density 300';
        $imgArguments .= ' -sharpen 0x.6';
        $imgArguments .= ' "' . $absFilePath . '"';
        $imgArguments .= '['. intval($pageNumber - 1) .']';
        $imgArguments .= ' "' . $absDestinationFilePath . '"';

        if(!file_exists($absDestinationFilePath)) {
            $command = CommandUtility::imageMagickCommand('convert', $imgArguments);
            CommandUtility::exec($command);
        }

        $thumbnail = substr($absDestinationFilePath, strlen(Environment::getPublicPath()));
        $templateVariableContainer->add($arguments['as'], $thumbnail);
        $output .= $renderChildrenClosure();
        $templateVariableContainer->remove($arguments['as']);

    }

    return $output;
}

编辑:

第三种方式:您可以使用JavaScript库动态生成缩略图。例如。 https://github.com/mozilla/pdf.js

© www.soinside.com 2019 - 2024. All rights reserved.