I have hundreds of PDFs that I want to crop. For each PDF, I have a unique set of coordinates around which to crop. I am trying to use the R's magick package (version ImageMagick 6.9.9.14), but I receive an error when importing a PDF.
This example from the magick documentation throws an error:
library(magick)
manual <- image_read('https://cran.r-project.org/web/packages/magick/magick.pdf', density = "72x72")
The error I receive is "Error in magick_image_readpath(path, density, depth, strip) : Magick: PDFDelegateFailed `The system cannot find the file specified.
' # error/pdf.c/ReadPDFImage/809"
When I check the config settings:
magick_config
I find that ghostscript is true. I am not sure if there are other settings required for reading in a PDF.
Has anyone else encountered a similar problem with magick? I am open to alternative packages with the ability to crop PDFs, if there are any.
I had the same problem on Windows. It was no R problem. In my case, I used ImageMagick 64-bit but had GhostScript 32-bit installed. After installing the 64-bit version of GhostScript it worked without any issues.
You could use tabulizer package.
library(tabulizer)
manual_url <- "https://cran.r-project.org/web/packages/magick/magick.pdf"
manual <- extract_text(manual_url)
For installing tabulizer follow exactly these steps.
Related
I am working on submitting an R package to CRAN. Right now I am trying to reduce the memory footprint of the package. Because this package deals with spatial data that has a very particular format, I want to include a properly formatted shapefile as an example. If I include the full-size original shapefile, there are no warnings (other than file size) in the R CMD checks. However, if I crop the file and include the cropped version in the package (in "inst/extdata") I get this warning:
W checking for executable files (389ms)
Found the following executable file:
inst/extdata/temp/temp.dbf
Source packages should not contain undeclared executable files.
See section ‘Package structure’ in the ‘Writing R Extensions’ manual.
This file is the database file associated with the shapefile. I have tried cropping the file and saving it using rgdal functions, sf functions, and using QGIS. I have also verified that the modes of the cropped files match the original file using chmod. I even tried changing .dbf to .DBF. Does anyone have any additional suggestions, other than listing it in BinaryFiles, which CRAN will not accept in a submission?
I'm running R version 4.0.2 via RStudio 2021.09.1 on Mac OSX 10.15.7. rgdal and sf are fully updated, as are all of their dependencies.
This is a known issue[1] where file will mis-identify DBF files with last-update date in the year 2022. Easiest fix is to not use a 2022 update date when saving the file. Alternatively you can simply change the second byte of the file after the fact, e.g.:
fn = "myfile.dbf"
sz = file.info(fn)$size
r = readBin(fn, raw(), sz)
r[2] = as.raw(121) ## make it 2021 instead of 2022
writeBin(r, fn)
(See also corresponding discussion on R-package-devel)
I'm using an M1, Big Sur Macbook. I need to embed the fonts of a number of pdfs that include plots from ggplot2.
However, when I run the embed_fonts() function, it returns the following error message: GhostScript was not found
With Homebrew, I installed Ghostscript. I have also reinstalled extrafont and extrafontdb, restarted RStudio, and then run font_import() and loadfonts() again. None of this solves the error message.
Have you experienced this problem? I wonder if it's because of the change to the M1's Apple Silicon?
I have also switched from using bash to zsh in my terminal. Could that have affected this?
A reprex:
library(ggplot2)
library(extrafont)
(plot <- ggplot(cars, aes(x = speed, y = dist)) +
geom_point())
ggsave("test_plot.pdf", plot)
embed_fonts(file = "test_plot.pdf", outfile = "test_plot_embedded.pdf")
I found the following two answers that seem related, but I'm unsure how to implement them:
How to fix "Unable to find GhostScript executable to run checks on size reduction" error upon package check in R?
R does not recognize GhostScript to embed eps plots
My issue was solved when I installed Ghostscript directly https://pages.uoregon.edu/koch/ (Ghostscript 9.54.0)
It appears there was, at least on my end, an issue using homebrew install ghostscript. When I ran that in Terminal, everything appeared to be fine. No error messages.
I am trying to convert this example pdf to png using the animation package the same way it´s done here Convert pdf to png in R
library("animation")
ani.options(outdir = "//Usuarios/Facturas")
pdf("Factura.pdf")
plot(1:10)
dev.off()
im.convert("Factura.pdf",
output = "Factura.png")
But I have this error:
Same error than here: Error using magick R to import PDF but that solution makes reference to an external software that I don´t find in the original post (and I haven´t installed). I red that Ghostscript do what I want (pdf to png), but isn´t what animation package suppose to do?
Or I messed up my brain?
Thanks
The animation package uses ImageMagick for some of its features (which is why ImageMagick is listed as a system requirement in the manual). ImageMagick in turn apparently uses Ghostscript for PDF rendering. So yeah: the Ghostscript issues discussed here are certainly worth looking into.
Finally I found the answer with another package, I am gonna explain if someone need it.
library(pdftools)
library(magick)
fichero="Factura.pdf"
png= pdftools::pdf_convert(fichero, dpi = 600)
# And if someone want to read it
png2 = image_read(png)
This doesn´t save any file .png, which is a perfect solution for my situation.
I'm attempting to use the magick R package to do some image editing. However, I am unable to read in GeoTIFF files. When I try this:
magick::image_read(RGBFile)
I get the following error message:
Error in magick_image_readpath(path, density, depth, strip) :
Magick: Unknown field with tag 34737 (0x87b1) encountered.
`TIFFReadDirectory' # warning/tiff.c/TIFFWarnings/912
After doing some research, I've concluded that the GeoTIFF tags are the issue.
I've written to the package maintainer to see if an update to the package might allow GeoTIFFs to be read, but in the meantime, is anyone aware of a way of an way of removing GeoTIFF tags without re-copying the entire file? (Or any other efficient workaround for dealing with large >1 GB images)
Thanks!
I have contacted the package maintainer and this bug has been fixed! It hasn't made it onto CRAN as of this posting but in the meantime installing magick with devtools::install_github("ropensci/magick") should do the trick.
I am making an R package using devtools and roxygen2. I can get a PDF manual using R CMD but I am really curious as to whether this can be done using devtools. devtools' build(), check(), install() all don't make a PDF manual. Is this tied to making vignettes?
I have read and referred to a similar thread Package development : location of pdf manual and vignette
After you install it, you can use:
pack <- "name_of_your_package"
path <- find.package(pack)
system(paste(shQuote(file.path(R.home("bin"), "R")),
"CMD", "Rd2pdf", shQuote(path)))
There is
devtools::build_manual()
Maybe also
devtools::check(manual=TRUE)
could work.
For the PDF manual of one specific function, you can run
fun <- "name_of_function"
help(fun, package = "name_of_package", help_type = "pdf")
system(paste0("open ", fun, ".pdf"))
assuming you have the package installed.