0

I would like to transform HTML into PDF using R

I have tried using cat and rmarkdown.

Below is an example from this answer, and my goal is to make it look the same as what it appears in the web

<p>You can use the <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/order.html" rel="noreferrer"><code>order()</code></a> function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the <code>example(order)</code> code:</p>

<pre><code>R&gt; dd[with(dd, order(-z, b)), ]
    b x y z
4 Low C 9 2
2 Med D 3 1
1  Hi A 8 1
3  Hi A 9 1
</code></pre>

<p><em>Edit some 2+ years later:</em>  It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the <code>order()</code> function:</p>

<pre><code>R&gt; dd[order(-dd[,4], dd[,1]), ]
    b x y z
4 Low C 9 2
2 Med D 3 1
1  Hi A 8 1
3  Hi A 9 1
R&gt; 
</code></pre>

<p>rather than using the name of the column (and <code>with()</code> for easier/more direct access).</p>
Kevin Ho
  • 57
  • 11
  • If you mean to convert an existing HTML page or file to PDF, you can print it to PDF in Chrome (or use `pagedown::chrome_print()`). In that case, I don't know how R Markdown is relevant. – Yihui Xie Dec 26 '19 at 04:18
  • Hi Yihui, my idea is to use something like rmarkdown::pandoc_convert("file.html", output = "with_weasyprint.pdf", to = "html5", options = c("--pdf-engine", "weasyprint")) – Kevin Ho Dec 27 '19 at 12:44
  • That's also fine. You can try both approaches and see which produces better PDF quality (I bet a real web browser, i.e., Chrome, would be better). – Yihui Xie Dec 27 '19 at 14:48

1 Answers1

2

Maybe R -e "rmarkdown::render('input.Rmd',output_file='output.pdf')"

So assuming you have this in a Rmakrdown file:

---
title: "R Notebook"
output:
  pdf_document: default
  html_notebook: default
---


You can use the [`order()`](http://stat.ethz.ch/R-manual/R-devel/library/base/html/order.html) function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the `example(order)` code:

```{r eval=FALSE}
  R> dd[with(dd, order(-z, b)), ]
    b x y z
  4 Low C 9 2
  2 Med D 3 1
  1  Hi A 8 1
  3  Hi A 9 1 
_``` 

Knitting it will result in a pdf that looks like this: enter image description here

Similarly, if you don't want to create a rmd but use the html, you can do: R -e "rmarkdown::pandoc_convert('input.html', output = 'output.pdf')"

Which will result in a pdf looking like this: enter image description here

novica
  • 605
  • 4
  • 11
  • Please edit your answer and add some context by explaining how your answer solve the problem in question, instead of posting code-only answer. – Pedram Parsian Dec 25 '19 at 05:06