【发布时间】:2017-11-01 04:54:39
【问题描述】:
我有一个 A4 大小的 pdf,中间有一张图片,周围有空白。我希望裁剪空白,只留下中间的数字。
我在 Windows 上使用 R,并且想要一些跨平台的独立 R 包解决方案。
魔术
我想使用 magick 包,但发现我似乎无法将 pdf 保存为矢量化(打开了一个问题 here)
knitr::plot_crop
我看到 knitr 有一个名为 plot_crop 的函数,它“使用 PDFCrop 或 ImageMagick 裁剪图(删除边缘)”使用它时我得到:
> knitr::plot_crop("plot.pdf")
pdfcrop: The Perl interpreter could not be found.
[1] "\"plot.pdf\""
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c pdfcrop "plot.pdf" "plot.pdf"' had status 1
2: In shell(paste(c(cmd, args), collapse = " ")) :
'pdfcrop "plot.pdf" "plot.pdf"' execution failed with error code 1
安装perl后,第一次运行代码出现如下错误:
> knitr::plot_crop("plot.pdf")
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
!!! Error: Ghostscript exited with error code 1!
======================================================================
starting package maintenance...
installation directory: C:\Users\junior\AppData\Roaming\MiKTeX\2.9
package repository: http://ftp.heanet.ie/mirrors/ctan.org/tex/systems/win32/miktex/tm/packages/
lightweight database digest: 8462e7c8aae10c40abda758bef7897f9
going to download 13358 bytes
going to install 3 file(s) (1 package(s))
downloading http://ftp.heanet.ie/mirrors/ctan.org/tex/systems/win32/miktex/tm/packages/pdfcrop.tar.lzma...
13358 bytes, 12.24 KB/Sec
extracting files from pdfcrop.tar.lzma...
======================================================================
[1] "\"plot.pdf\""
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c pdfcrop "plot.pdf" "plot.pdf"' had status 13
2: In shell(paste(c(cmd, args), collapse = " ")) :
'pdfcrop "plot.pdf" "plot.pdf"' execution failed with error code 13
然后第二次尝试运行代码时出现此错误:
> knitr::plot_crop("plot.pdf")
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
!!! Error: Ghostscript exited with error code 1!
[1] "\"plot.pdf\""
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c pdfcrop "plot.pdf" "plot.pdf"' had status 13
2: In shell(paste(c(cmd, args), collapse = " ")) :
'pdfcrop "plot.pdf" "plot.pdf"' execution failed with error code 13
还有其他选择吗?
我是否缺少其他已知解决方案? (谢谢)
【问题讨论】:
-
ImageMagick 可以像您一样修剪图像周围的空白区域。但是,不幸的是,ImageMagick 只是一个光栅处理器。因此,它将栅格化您的输入 PDF(通过 Ghostscript),修剪栅格数据,然后将栅格数据嵌入到矢量 PDF 外壳中。因此,您会丢失矢量数据。抱歉,我不知道有任何向量到向量处理器会修剪空白。您也许可以使用 potrace 重新矢量化,但我没有这样做的经验。
-
无赖,谢谢。我想知道是否有一些工具可以做到这一点。我只能找到 pdfcrop,但它似乎只适用于 linux。
标签: r pdf imagemagick knitr