Imagemagick SVG到PDF的转换图像质量不好 [英] Imagemagick SVG to PDF conversion image quality is bad

查看:97
本文介绍了Imagemagick SVG到PDF的转换图像质量不好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试将SVG(宽度737,高度521)转换为A4尺寸的PDF.问题在于生成的图像质量真的很差.

We are trying to convert an SVG (width 737, height 521) to PDF of A4 dimensions. The problem is that the quality of images generated is really bad.

这就是我们正在做的

SVG(带有远程图像URL):

SVG (with remote image URLs):

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="737" height="521">
    <g class="background">
        <title>Background</title>
        <image x="0" y="0" width="737" height="521" id="svg_1" xlink:href="http://static.inkive.com/assets/img/backgrounds/default.svg"/>
    </g><g class="main">
        <title>Main</title>
        <image id="svg_2" clip-path="url(#clip_svg_2)" class="layoutBox" height="146" width="195" y="185" x="112" xlink:href="https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-ash3/t1/1779720_10153782785350029_1577015767_n.jpg">112 185 195 146</image>
        <image id="svg_3" clip-path="url(#clip_svg_3)" class="layoutBox" height="146" width="195" y="342" x="112" xlink:href="https://scontent-b.xx.fbcdn.net/hphotos-frc3/t1/1526323_10153667389170029_829908430_n.jpg">112 342 195 146</image>
        <image id="svg_4" clip-path="url(#clip_svg_4)" class="layoutBox" height="146" width="195" y="28" x="112" xlink:href="https://scontent-b.xx.fbcdn.net/hphotos-prn1/t1/1522194_10153655638625029_2110669828_n.jpg">112 28 195 146</image>
        <image id="svg_5" clip-path="url(#clip_svg_5)" class="layoutBox" height="222" width="296" y="28" x="323" xlink:href="https://scontent-a.xx.fbcdn.net/hphotos-prn2/t1/1157459_10153637913840029_1004079041_n.jpg">323 28 296 222</image>
        <image id="svg_6" clip-path="url(#clip_svg_6)" class="layoutBox" height="222" width="296" y="266" x="323" xlink:href="https://scontent-a.xx.fbcdn.net/hphotos-frc3/t1/996689_10153637905215029_532085859_n.jpg">323 266 296 222</image>
    </g>
</svg>

我们正在下载图像并创建带有本地链接的SVG,因为imagemagick的SVG到PDF的转换由于远程URL而失败.这是带有本地链接的SVG-

We are downloading the images and creating an SVG with local links as imagemagick's SVG to PDF conversion fails with remote URLs. Here's the SVG with local links-

<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="737" height="521">
    <g class="background">
        <title>Background</title>
        <image x="0" y="0" width="737" height="521" id="svg_1" xlink:href="file:///tmp/119810194_default.svg"/>
    </g><g class="main">
        <title>Main</title>
        <image id="svg_2" clip-path="url(#clip_svg_2)" class="layoutBox" height="146" width="195" y="185" x="112" xlink:href="file:///tmp/119810194_1779720_10153782785350029_1577015767_n.jpg">112 185 195 146</image>
        <image id="svg_3" clip-path="url(#clip_svg_3)" class="layoutBox" height="146" width="195" y="342" x="112" xlink:href="file:///tmp/119810194_1526323_10153667389170029_829908430_n.jpg">112 342 195 146</image>
        <image id="svg_4" clip-path="url(#clip_svg_4)" class="layoutBox" height="146" width="195" y="28" x="112" xlink:href="file:///tmp/119810194_1522194_10153655638625029_2110669828_n.jpg">112 28 195 146</image>
        <image id="svg_5" clip-path="url(#clip_svg_5)" class="layoutBox" height="222" width="296" y="28" x="323" xlink:href="file:///tmp/119810194_1157459_10153637913840029_1004079041_n.jpg">323 28 296 222</image>
        <image id="svg_6" clip-path="url(#clip_svg_6)" class="layoutBox" height="222" width="296" y="266" x="323" xlink:href="file:///tmp/119810194_996689_10153637905215029_532085859_n.jpg">323 266 296 222</image>
    </g>
</svg>

我们正在将该SVG保存到文件中,并正在运行以下命令:

We are saving this SVG to a file and are running the following command:

convert -density 600 /var/www/development/assets/img/uploads/119810194_1.svg -resize 3508x2480 /var/www/development/assets/img/uploads/119810194_1.pdf

输出

如您所见,输出PDF中的图像质量非常差.

As you can see, the quality of images in the output PDF is very bad.

有人可以帮我吗?要生成具有更好图像的72 DPI和300 DPI PDF,需要做些什么?

Can anyone help me with this? What needs to be done to generate 72 DPI and 300 DPI PDF with better images?

我们也在setResolution(300,300)中使用PHP进行了尝试,但是结果是相同的.

We have tried this with PHP as well, with setResolution(300, 300) but the result was the same.

推荐答案

您不应使用-resize,因为这将在栅格化后 完成.您必须计算正确的浓度值(以72dpi为基础).然后,如果长宽比不适合A4纸张,则可以-crop.

You should not use -resize because that will be done after rasterization. You have to calculate the correct density value (use 72dpi as base). Then you can -crop if the aspect ratio won't fit on your A4 paper.

A4纸的尺寸为8.27 * 11.7英寸.使用300 dpi时,较长的一面为11.7 * 300(3510)点 ,这是原始尺寸(3510/737)的4.76倍.解码时,您必须将密度乘以该倍数.将默认值72乘以4.76将得出343.

Size of the A4 paper is 8.27 * 11.7 inches. Using 300 dpi, the longer side is 11.7 * 300 (3510) dots which is 4.76 times the original size (3510 / 737). You have to multiply the density by this much when decoding. Multiplying the default 72 by 4.76 will give you 343.

convert -density 343 in.svg out.pdf

这应该给您合适的尺寸.

This should give you the right size.

这篇关于Imagemagick SVG到PDF的转换图像质量不好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆