【问题标题】:Crop whitespace pdf using R (any standalone cross platform solutions?)使用 R 裁剪空白 pdf(任何独立的跨平台解决方案?)
【发布时间】: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


【解决方案1】:

错误信息说:

pdfcrop: 找不到 Perl 解释器。

你必须安装 Perl:https://www.perl.org/get.html

【讨论】:

  • (ps:我在发布问题之前已经安装了 ActiveState Perl - 也许我应该重新启动计算机或其他东西,因为它可能在 PATH 上看不到 perl)
  • 是的,您需要确保perlPATH 上。
  • 所以重新启动 Windows 已将所需的部分添加到 PATH 中,但现在出现了一个新错误,请参阅上面的原始问题。
  • 它在 Windows 上运行良好。 system("pdfcrop --version") 返回 v1.38。我不知道你的电脑出了什么问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多