r - 将 PDF 中未嵌入的字体替换为同系列其他字体的最佳方法是什么?

标签 r pdf fonts latex embedding

我没有意识到 R 制作的统计图表和图表使用了我的 Windows 计算机上未安装的字体,这些是:
Helvetica
Helvetica-Bold
Helvetica-Oblique
ZapfDingbats

我发现通过运行 pdffonts <file.name.pdf>从命令行。所以这些字体没有嵌入到 PDF 文件中。

我已将论文发送打印,但打印厂说这些字体需要嵌入到 PDF 文件中。我用 LaTeX 写的并包含 PDF 格式的图表。

如何用非常相似的字体替换或替代这些字体(有些已获得许可)而不扭曲各个 PDF 中的图形?我不打算使用商业实用程序。

注释
- Windows 7 32 位
- 这篇文章类似:

https://superuser.com/a/223687/212779

但是,它是用商业程序完成的,并且现在已经相对较旧了。可能会有更好的新想法来克服这个问题。但愿如此。

到目前为止我的试用
我已经成功地使用 Ghostscript 中的一些专家在以下帖子中的过程进行了替换:https://superuser.com/q/39167/212779

这需要 Ghostscript要安装(我有版本 9.15)加上 Adob​​e PostScript对于 Windows,通用脚本和命令行脚本:

gswin32c ^
 -dNOPAUSE ^
 -dBATCH ^
 -sDEVICE=pdfwrite ^
 -dPDFSETTINGS=/prepress ^
 -dCompatibilityLevel=1.4 ^
 -dHaveTrueTypes=true ^
 -dSubsetFonts=true ^
 -sOutputFile="c:\path\to\somename.pdf" ^
 -c ".setpdfwrite <</NeverEmbed [ ]>> setdistillerparams" ^
 -f "c:\path\to\somename.ps"

问题
如何获取生成的.ps postscript 文件以及随后的 .pdf文件(现在优雅地嵌入了类似的字体)与原始 PDF 文件具有相同的大小,即裁剪为与原始 PDF 文件完全相同的尺寸?

如果我保留 Adob​​e Reader(版本 XI)中的所有默认设置(不要与商业 Adob​​e Professional 混淆),我会在 .ps 中获得相同的尺寸。文件,所以我真正需要的是 Ghostscript 中的一些代码来保留 .ps 的尺寸生成最终PDF文件时,有什么帮助吗?顺便说一句,我打开.ps文件 SumatraPDF观众。

最佳答案

您很接近:之前的一个 SO 问题 ( 12857849 ) 提到将 -dEmbedAllFonts=true 添加到 gswin32c 的命令行。

您的第二个问题(在评论中)可以通过多种方式完成批处理多个 PDF,“最佳”是您最容易理解、维护和使用您可以随时访问的工具。

在 R 中:

fnames <- list.files(pattern = 'doctorate-.*.pdf')
for (fn in fnames) {
    ofn <- paste0(gsub('\\.pdf$', '', fn, ignore.case = TRUE), '-withfonts.pdf')
    message('Processing: ', fn, ' --> ', ofn)
    system2('gswin32c',
            paste0('-dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress ',
                   '-dCompatibilityLevel=1.4 -dHaveTrueTypes=true -dSubsetFonts=true ',
                   '-dEmbedAllFonts=true -sOutputFile="', ofn, '" ',
                   '-c ".setpdfwrite <</NeverEmbed [ ]>> setdistillerparams" ',
                   '-f "', fn, '"'))
}

这有点冗长。如果您在 system2 中添加 stdout=FALSE,您应该看到如下内容:

Processing: doctorate-1.pdf --> doctorate-1-withfonts.pdf
Processing: doctorate-2.pdf --> doctorate-2-withfonts.pdf
Processing: doctorate-3.pdf --> doctorate-3-withfonts.pdf

bash中:

for fn in doctorate-*.pdf ; do
    ofn="$(basename ${fn} .pdf)-withfonts.pdf"
    echo "Processing: ${fn} --> ${ofn}"
    gswin32c -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress ^
             -dCompatibilityLevel=1.4 -dHaveTrueTypes=true -dSubsetFonts=true ^
             -dEmbedAllFonts=true -sOutputFile="${ofn}" ^
             -c ".setpdfwrite <</NeverEmbed [ ]>> setdistillerparams" ^
             -f "${fn}"
done

(请注意,如果您在 Windows 中 msys2 下的 bash 终端中执行此操作,则可能会失败,因为 msys2 看到“/prepress”并假设它是一个路径,将其转换为 c:/msys64/prepress ,这显然对 gs 来说毫无意义......)

关于r - 将 PDF 中未嵌入的字体替换为同系列其他字体的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28568847/

相关文章:

r - 将 df 中的值更改为 0 = FALSE、1 = TRUE、2 = TRUE

R 计数值在每行中出现的次数

css - 在没有 htaccess/iis 的元素中使用自定义字体会导致 "Resource interpreted as Font but transferred with MIME type text/html"

r - 如何在 gtable 内部使用解析并保留尾随零

r - 如果函数中的格子

tensorflow - 如何获取tensorflow文档的pdf格式?

c# - 使用ITextSharp从两个分隔线之间的PDF中提取文本

pdf - 使用 angular2/typescript 从 html 生成 PDF 文件

r - 使用 interact_plot(包交互)更改图形的字体

html - 为什么我对该网站字体的呈现会产生不同的结果?