<p>This week we released version <code>1.0</code> of the ropensci <a href="https://cran.r-project.org/web/packages/pdftools/index.html">pdftools</a> package to CRAN. Pdftools provides utilities for extracting text, fonts, attachments and other data from PDF files. It also supports rendering of PDF files into bitmap images.</p>
<p>This release has a few internal enhancements and fixes an annoying bug for landscape PDF pages. The version bump to <code>1.0</code> signifies that the package has undergone sufficient testing and the API is stable.</p>
<h2>Extracting Text</h2>
<p>As described in our previous <a href="https://ropensci.org/blog/blog/2016/03/01/pdftools-and-jeroen">post</a>, the most common use of <code>pdftools</code> is extracting text from (scientific) articles for searching / indexing. But let's try a somewhat more unusual PDF file this time: a poster.</p>
<div><pre><code><span>library</span><span>(</span>pdftools<span>)</span>
url <span>&lt;-</span> <span>&quot;https://www.rstudio.com/wp-content/uploads/2016/02/advancedR.pdf&quot;</span>

<span># Display author, editor</span>
pdf_info<span>(</span><span>url</span><span>)</span>
</code></pre></div>
<p>The <code>pdf_info</code> file returns all kind of metadata from the pdf file. For example we can read that this PDF was created on 2016-02-12 by Arianne Colton using Acrobat PDFMaker 11 for PowerPoint.</p>
<div><pre><code># extract text vector
text &lt;- pdf_text(url)

# Print text from page 1
cat(text[1])
</code></pre></div>
<p>The <code>pdf_text</code> function extracts text into an R character vector if length equal to the number of pages in the PDF.</p>
<p>Note how the text is spaced to match the position in the PDF page.</p>
<h2>Rendering PDF</h2>
<p>Recent versions of pdftools allow rendering of PDF pages into bitmap images. The <code>pdf_render_page</code> function returns the bitmap as a raw vector array of size channels * width * height (in pixels).</p>
<div><pre><code><span>library</span><span>(</span>pdftools<span>)</span>
bitmap <span>&lt;-</span> pdf_render_page<span>(</span><span>url</span><span>,</span> page <span>=</span> <span>1</span><span>,</span> dpi <span>=</span> <span>72</span><span>)</span>
<span>dim</span><span>(</span>bitmap<span>)</span>
<span>## 4 1100 850</span>
</code></pre></div>
<p>From here we can use for example the rOpenSci <a href="https://cran.r-project.org/web/packages/magick/vignettes/intro.html">magick</a> package to read the bitmap and manipulate/export it to various formats.</p>
<div><pre><code><span>library</span><span>(</span>magick<span>)</span>
poster <span>&lt;-</span> image_read<span>(</span>bitmap<span>)</span>
<span>print</span><span>(</span>poster<span>)</span>
image_write<span>(</span>poster<span>,</span> <span>&quot;out.png&quot;</span><span>,</span> format <span>=</span> <span>&quot;png&quot;</span><span>)</span>
</code></pre></div>
<p>Or have some fun with the other magick tools :)</p>
<div><pre><code><span># Download dancing banana</span>
banana <span>&lt;-</span> image_read<span>(</span><span>&quot;https://jeroenooms.github.io/images/banana.gif&quot;</span><span>)</span>
banana <span>&lt;-</span> image_scale<span>(</span>banana<span>,</span> <span>&quot;300&quot;</span><span>)</span>

<span># Combine and flatten frames</span>
frames <span>&lt;-</span> <span>lapply</span><span>(</span>banana<span>,</span> <span>function</span><span>(</span>frame<span>)</span> <span>{</span>
  image_composite<span>(</span>poster<span>,</span> frame<span>,</span> offset <span>=</span> <span>&quot;+70+30&quot;</span><span>)</span>
<span>})</span>

<span># Turn frames into animation</span>
animation <span>&lt;-</span> image_animate<span>(</span>image_join<span>(</span>frames<span>))</span>
<span>print</span><span>(</span>animation<span>)</span>

<span># Save as gif</span>
image_write<span>(</span>animation<span>,</span> <span>&quot;output.gif&quot;</span><span>)</span>
</code></pre></div>