【问题标题】:Using pdftools package and png package to convert pdf to png image gives incomplete png使用 pdftools 包和 png 包将 pdf 转换为 png 图像给出不完整的 png
【发布时间】:2018-02-13 21:32:05
【问题描述】:

我有这张荷兰的 pdf 图片 点:

pdf image of the netherlands with dots

当我尝试将此 pdf 转换为 png 图像时,使用 pdftoolspng 像这样:

library(png)
library(pdftools)
bitmap <- pdf_render_page("netherlands.pdf", page = 1, dpi = 300)
png::writePNG(bitmap, "netherlands.png"))

结果是这样的:

图像没有 pdf 版本中显示的点。

我该如何解决这个问题?

这是我的会话信息:

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2008 R2 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252    LC_MONETARY=Dutch_Netherlands.1252
[4] LC_NUMERIC=C                       LC_TIME=Dutch_Netherlands.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] png_0.1-7               pdftools_1.4            psych_1.6.9             psy_1.1                 maptools_0.8-41        
 [6] rgeos_0.3-21            rgdal_1.2-5             sp_1.2-4                tagcloud_0.6            Rcpp_0.12.12           
[11] wordcloud_2.5           directlabels_2015.12.16 extrafont_0.17          haven_1.0.0             tm_0.6-2               
[16] NLP_0.1-9               qdap_2.2.5              RColorBrewer_1.1-2      qdapTools_1.3.1         qdapRegex_0.6.0        
[21] qdapDictionaries_1.0.6  tidyr_0.6.1             dplyr_0.5.0             readxl_1.0.0            reshape2_1.4.2         
[26] Hmisc_4.0-2             Formula_1.2-1           survival_2.40-1         lattice_0.20-34         knitr_1.15.1           
[31] ggthemes_3.4.0          ggplot2_2.2.1           rmarkdown_1.4          

loaded via a namespace (and not attached):
 [1] splines_3.3.2       gender_0.5.1        gtools_3.5.0        assertthat_0.1      latticeExtra_0.6-28 xlsxjars_0.6.1     
 [7] cellranger_1.1.0    yaml_2.1.14         slam_0.1-40         Rttf2pt1_1.3.4      backports_1.0.4     quadprog_1.5-5     
[13] extrafontdb_1.0     chron_2.3-48        digest_0.6.12       colorspace_1.3-2    htmltools_0.3.5     Matrix_1.2-7.1     
[19] plyr_1.8.4          XML_3.98-1.5        scales_0.4.1        gdata_2.17.0        htmlTable_1.7       tibble_1.2         
[25] openNLP_0.2-6       reports_0.1.4       nnet_7.3-12         lazyeval_0.2.0      mnormt_1.5-5        magrittr_1.5       
[31] evaluate_0.10       foreign_0.8-67      tools_3.3.2         data.table_1.10.0   stringr_1.1.0       xlsx_0.5.7         
[37] munsell_0.4.3       cluster_2.0.5       plotrix_3.6-4       RCurl_1.95-4.8      igraph_1.0.1        labeling_0.3       
[43] bitops_1.0-6        base64enc_0.1-3     venneuler_1.1-0     gtable_0.2.0        DBI_0.5-1           R6_2.2.0           
[49] gridExtra_2.2.1     openNLPdata_1.5.3-2 rprojroot_1.1       rJava_0.9-8         stringi_1.1.2       parallel_3.3.2     
[55] rpart_4.1-10        acepack_1.4.1

【问题讨论】:

    标签: r pdf png


    【解决方案1】:

    更新: pdf 在 MacOS 上呈现良好(见下文)。我认为问题在于 pdf 文件使用的字体在 Windows 上不可用。在 Windows 上我看到了:

    > pdf_fonts("~/../Downloads/netherlands_pdf.pdf")
    Warning: error: Couldn't find a font for 'ZapfDingbats', subst is 'Helvetica'
              name  type embedded                          file
    1 ZapfDingbats type1    FALSE C:\\Windows\\Fonts\\arial.ttf
    

    在渲染我看到的 PDF 时:

    > bitmap <- pdf_render_page("~/../Downloads/netherlands_pdf.pdf")
    Warning: error: No display font for 'ArialNarrow'
    Warning: error: No display font for 'ArialNarrow,Bold'
    Warning: error: No display font for 'ArialNarrow,Italic'
    Warning: error: No display font for 'ArialNarrow,BoldItalic'
    Warning: error: No display font for 'ArialNarrow-Bold'
    Warning: error: No display font for 'ArialNarrow-Italic'
    Warning: error: No display font for 'ArialNarrow-BoldItalic'
    Warning: error: No display font for 'HelveticaNarrow'
    Warning: error: No display font for 'HelveticaNarrow,Bold'
    Warning: error: No display font for 'HelveticaNarrow,Italic'
    Warning: error: No display font for 'HelveticaNarrow,BoldItalic'
    Warning: error: No display font for 'HelveticaNarrow-Bold'
    Warning: error: No display font for 'HelveticaNarrow-Italic'
    Warning: error: No display font for 'HelveticaNarrow-BoldItalic'
    Warning: error: No display font for 'BookAntiqua'
    Warning: error: No display font for 'BookAntiqua,Bold'
    Warning: error: No display font for 'BookAntiqua,Italic'
    Warning: error: No display font for 'BookAntiqua,BoldItalic'
    Warning: error: No display font for 'BookAntiqua-Bold'
    Warning: error: No display font for 'BookAntiqua-Italic'
    Warning: error: No display font for 'BookAntiqua-BoldItalic'
    Warning: error: No display font for 'ArialUnicode'
    

    所以我怀疑这些点实际上是使用一种字体(可能是 dingbat)打印的,但如果这种字体不可用,它们就无法呈现。

    在 OSX 上没有此类警告,并且输出似乎正确:

    但是我不确定为什么 pdf 在 Windows 上的 Chrome 中正确显示。也许铬包括额外的字体?

    【讨论】:

    • 感谢 Jeroen 的快速回复!我一回到我的电脑就会试试这个。
    • 我明天早上去看看。地图是ggplot2生成的,我不知道这个dingbat字体。也许这个问题与我昨天发布的这个问题有关:stackoverflow.com/q/46034457/1983395
    • 我查看了生成地图的ggplot2 代码。我只使用geom_point() 作为点。我无法解释 pdf 提供的 dingbat 字体。
    • 如果你使用 ggsave 你可能需要先设置pdf.options(useDingbats=FALSE)
    • 解决了!如果您将pdf.options(useDingbats=FALSE) 添加到您的答案中,我将非常乐意接受。非常感谢您的帮助!
    猜你喜欢
    • 2019-11-01
    • 2010-10-13
    • 2017-06-13
    • 2013-12-27
    • 2011-02-21
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    相关资源
    最近更新 更多