3

I have a folder with multiple JPEG files. How do I generate a PDF file from these JPEGs in R?

One JPEG = 1 PDF page. Images are of the same size.

Thanks a lot in advance.

  • Why do you want to use R to do this? – A5C1D2H2I1M1N2O1R2T1 Jan 30 '13 at 10:06
  • I have an R script that downloads JPEGs automatically, want to put them into a PDF. Would like to do everything with one tool. – dmitryungurean Jan 30 '13 at 10:13
  • Does using a `system` call to something like Ghostscript count as using "one tool"? ;) – A5C1D2H2I1M1N2O1R2T1 Jan 30 '13 at 10:15
  • Is there no native support of PDF generation in R? Why use Ghostscript? – dmitryungurean Jan 30 '13 at 10:21
  • @dmitryungurean There is PDF generation in R, but it's simply an output mode for graphics, it doesn't - AFAIK - support any control over the structure of the produced document. I would have the R script call some kind of external tool to do this. – Jack Aidley Jan 30 '13 at 10:32
  • It's not the native support for PDF generation that bothers me. It is the effort that would be required to read the JPEGs in in a format that R can recognize before being able to plot it as a PDF. Using Ghostscript it's as easy as specifying your output file and providing a list of input files that need to be combined. – A5C1D2H2I1M1N2O1R2T1 Jan 30 '13 at 10:32
  • Would [this Q&A](http://stackoverflow.com/questions/9543343/plot-a-jpg-image-using-base-graphics-in-r) help with reading in the jpegs? It sounds like @AnandaMahto might be justified in suggesting Ghostscript from a performance standpoint, though. – BenBarnes Jan 30 '13 at 10:45

3 Answers3

1

You can do this easily using Latex. Which is nice, because then you can just use Sweave to do the whole thing.

You can do something along the lines of :

% This is some Sweave file
\documentclass{article}
\usepackage{graphicx}
\begin{document}

<<results=tex,echo=FALSE>>=
mypics <- dir('mypics')
for(i in mypics){
cat("\\includegraphics{", i, "}\n\n", sep = "")
}
@
\end{document}

OK, you'll have to set up your Sweave pipeline, but with a bit of tweaking you can automate the whole process easily.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
1

if you insist on using R (other tools are more suitable), try something like this (slow, untested):

   lf = list.files(pattern = "jpeg") # image filenames

   library(jpeg)
   jpegs = lapply(lf, readJPG)

   library(grid)
   pdf("output.pdf", width=8, height=4)
   grid.raster(jpegs[[1]])
   lapply(jpegs[-1], function(x) {grid.newpage() ; grid.raster(x)} ) -> bquiet
   dev.off()
baptiste
  • 75,767
  • 19
  • 198
  • 294
0

If you insist on using R to do this then you can open a pdf plotting device, par to set the margins (default will probably be to big and not centering), then in a loop use plot.new to start a new page and plot.window to set up the coordinates, etc. without plotting axes etc., use the read.jpeg function from the ReadImages package (or other tool/package to read, EBImage is another possibility) then rasterImage to plot the jpeg to the pdf device (or replace some of those steps with other image plotting functions, such as the plot method in ReadImages).

But overall it is probably easier/quicker/better/... to use a tool better designed for this type of thing. The ImageMagick suite of programs comes to mind, LaTeX has also been mentioned, and there are probably other tools as well.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110