-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
173 lines (124 loc) · 5.02 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# ghostpdf
<!-- badges: start -->
<!-- badges: end -->
Ghostpdf wraps around [ghostscript](https://www.ghostscript.com/) to provide easy manipulation functions for pdf files. With Ghostpdf you can merge documents, decrypt them, encrypt them back, transform pdfs into images or extract pages from a pdf.
> **Note** The idea is to eventually have the same functions applicable with PostScript files too.
## Installation
You can install the development version of ghostpdf from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("RodrigoZepeda/ghostpdf")
```
You will also need to install **ghostpdf** either from their page [https://www.ghostscript.com/releases/gsdnld.html](https://www.ghostscript.com/releases/gsdnld.html) or if you are in a Unix system with a package manager such as:
+ **OSX** `brew install ghostscript`
+ **Ubuntu/Debian** `sudo apt-get install ghostscript`
+ **CentOS/Fedora** `sudo yum install ghostscript`
OSX users who are not familiar with homebrew open terminal and copy:
```{bash, eval = FALSE}
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
after the installation is completed, reopen the terminal and write:
```{bash, eval = FALSE}
brew install ghostscript
```
## Examples
```{r libcall}
library(ghostpdf)
#We'll generate some pdf documents for the examples
colors <- rainbow(9)
for (i in 1:9){
pdf(paste0("Example",i,".pdf"), width = 6, height = 4)
plot(x = rnorm(100), y = rnorm(100), col = colors[i], main = paste0("Page ", i))
dev.off()
}
```
### Merging pdfs
You can merge multiple pdf files into a single file:
```{r merge}
#Merge them
pdf_merge(output_file = "All_pdfs.pdf")
#Or merge some of them:
pdf_merge(c("Example1.pdf", "Example6.pdf"), output_file = "Some_pdfs.pdf")
```
### Extracting pages from a pdf
One can extract some of the pages from a pdf:
```{r extract}
#Extract from page 2 to 4
pdf_extract("All_pdfs.pdf", first_page = 2, last_page = 4,
output_file = "All_pdfs_subset.pdf")
#Or pages 1, 5 and 8
pdf_extract("All_pdfs.pdf", page_sequence = c(1,5,8),
output_file = "Other_pdfs_subset.pdf")
```
### Shrinking a pdf
You can reduce the size of a pdf with `pdf_shrink` which enables compression and other memory-saving tricks:
```{r shrink}
#Check current file size:
original_size <- file.info("All_pdfs_subset.pdf")["size"]
#Shrink file
pdf_shrink("All_pdfs_subset.pdf")
#Check new file size
reduced_size <- file.info("All_pdfs_subset.pdf")["size"]
#Verify size is smaller
#in this case there isn't much to gain because the pdf was small by itself
#in large pdfs this is useful
message(paste0("Shrank pdf from ", original_size, " to ", reduced_size))
```
### PDF to image
Transform a pdf (or select) into images in any of the following formats: `png`, `tiff`, `jpeg`, `fax`, `pcx`, `bmp`, `psd`:
```{r image}
#Transform a pdf into png
pdf_to_image("Some_pdfs.pdf", output_file = "cool_plots.png")
#Or sections of another pdf
pdf_to_image("All_pdfs.pdf", first_page = 4, last_page = 7, output_file = "cooler_plots.jpg")
#Sections can also be specified with a vector
pdf_to_image("All_pdfs.pdf", page_sequence = c(1,5,8,9),
output_file = "cooler_plots.tiff")
#You can also specify the imagedevice for example to grayscale
pdf_to_image("All_pdfs.pdf", page_sequence = c(1:4, 7),
image_device = "pnggray",
output_file = "gray_plots.png")
#Additional options can be passed in additional_flags e.g. transparent background
pdf_to_image("All_pdfs.pdf", page_sequence = 2,
image_device = "pngalpha",
output_file = "nbg.png",
additional_flags = c("-dBackgroundColor=16#fffff",
"-dDownScaleFactor=3"))
```
### Encrypt / decrypt pdf
You can encrypt a pdf file with:
```{r}
pdf_encrypt("Example1.pdf", password = "mypassword")
```
To create a decrypted copy:
```{r}
pdf_decrypt("Example1.pdf", password = "mypassword")
```
### More
I would like to eventually add same functionality for eps files and to transform between images and image to pdf.
### Troubleshooting
+ **Can't find ghostscript on windows**
1) I would recommend executing R as administrator. That's the easier (unsafe) way.
2) Open `cmd` and write `where gswin64c` or `where gswin32c`. One of them should work. The path returned has to be used as `gs_path` in functions:
```{r, eval = FALSE}
pdf_merge(output_file = "All_pdfs.pdf", gs_path = "C:/Program Files/gs/gs9.56.1/bin/gswin64c.exe")
```
```{r, echo = FALSE, results = 'hide'}
list.files(pattern = "Example") |> file.remove()
list.files(pattern = ".*.png") |> file.remove()
list.files(pattern = ".*.pdf") |> file.remove()
list.files(pattern = ".*.tiff") |> file.remove()
list.files(pattern = ".*.jpg") |> file.remove()
```