<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/announcing-pdftools-1-0/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ropensci.org/blog/technotes/2016/12/09/pdftools-10"> rOpenSci Blog - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This week we released version <code>1.0</code> of the ropensci <a target="_blank" rel="nofollow" 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 target="_blank" rel="nofollow" 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 data-lang="r"><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 data-lang="text"># 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 data-lang="r"><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 target="_blank" rel="nofollow" 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 data-lang="r"><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 <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png"></p>
<div>
<pre><code data-lang="r"><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>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ropensci.org/blog/technotes/2016/12/09/pdftools-10"> rOpenSci Blog - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/c2b9uPku4ho"></a></div>