2

我正在做一个需要用给定大小的矩形拉伸文本的项目。我为此尝试了imagick,但它有一些质量损失。

例如,我有一个尺寸为 203x78(任何尺寸)的盒子,并写了一个文本“LOREM IPSUM LOREM”(任何文本)。我希望这个文本完全适合我给定的 203x78 大小,无论字体大小,无论其他任何东西,只要质量和完全适合我的盒子。

我找不到比 imagick 更好的方法了:首先绘制与我的盒子最接近的字体大小的透明文本(肯定更小),然后绘制图像。之后,将其调整为我的盒子大小。调整大小时,我尝试了 imagick 中的任何过滤器。除了小文本之外,没有任何问题。例如。字体大小为 20 像素,而我的某些框大小大于在任何一侧绘制约 1-3 像素。我的意思是,20px 绘制的文本大小是 40x18,但我的框是 41x20。因此将其大小调整为 41x20,但在任何效果和效果值上都变得模糊。

所以我想用 TCPDF 创建我的文本,并想调整文本大小以适应任何给定的大小。配件必须完全适合水平和垂直,完全拉伸。我选择 PDF 是因为 PDF 中的文本为矢量格式,并且在调整任何大小时都没有损失。

TCPDF 有一个水平拉伸,但我找不到任何方法来拉伸两侧。

附加信息:需要使用 ttf 文件创建文本。需要透明的 png 输出。水平拉伸不能与字符间距,所有行为必须与矢量调整大小相同,就像 ADOBE ILLUSTRATOR 一样。

Illustrator 调整文本大小,任意大小

尝试:我为此使用了 php gd,但质量比 imagick 差。我试图安装我的服务器 php-cairo 但做不到。我搜索了 inkscape 来执行此操作,但找不到在命令行上工作的方法。我尝试了 imagick output > inkscape 来转换矢量,但完全失败了。我尝试了 TCPDF 输出 > imagick 来调整大小,但质量不高(imagick 没有矢量调整大小的能力)

帮助!

4

2 回答 2

3

ImageMagick cannot be used to do that, because it is a "raster image processor". So in anyway, the image is gonna be rasterized before any operation (see Vector Image formats).

I achieved to get something pretty clean with inkscape.

First of all, we'll need a simple svg file containing the text. Such a file can easily generated with PHP or any other language:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   version="1.1">
  <g>
    <text
       xml:space="preserve"
       style="font-size:40px;
              font-style:normal;
              font-weight:normal;
              line-height:125%;
              letter-spacing:0px;
              word-spacing:0px;
              fill:#000000;
              fill-opacity:1;
              stroke:none;
              font-family:Helvetica"
       x="0"
       y="0"
       sodipodi:linespacing="125%"><tspan
         sodipodi:role="line"
         x="0"
         y="0">Hello World!</tspan></text>
  </g>
</svg>

From there, we can easily change the text, font, color... then the following command line do the trick (by adjusting the -w and -h parameters):

inkscape filename.svg --export-png=filename.png -w800 -h300 --export-area-drawing
于 2013-07-03T11:56:36.780 回答
0

我发现的一种方法是进行转换并输出 html

$pdf->StartTransform();

$pdf->Scale(100,150);

// Output html.
$html=('sample text');
$pdf->writeHTMLCell($w=50, $h=10, $x='', $y='', $html, $border=1, $ln=1, $fill=0, $reseth=true, $align='C', $autopadding=true);


$pdf->StopTransform();
于 2014-11-21T17:15:38.693 回答