title: DT::datatable() export to pdf while preserving HTML </br> for multiline cells
body: <p>Trying to use <code>DT::datatable()</code> in R to export a table with multiline cells to pdf. <code>DT::datatable()</code> works perfectly for multiline cells, but problems arise when trying to export to pdf. I’ve tried two different approaches, each with their own problems.
  There are  very many threads about keeping formatting when exporting to pdf, but none about exporting multiline cells.</p>
  <p><strong>Approach 1:</strong> The pdf print button of the <code>Buttons</code> extension with the arguments <code>escape = FALSE</code> and <code>exportOptions = list(stripHtml = FALSE)</code> and customized <code>JS()</code> functions.</p>
  <p>The problem here is that the formatting is not preserved (in Rstudio it looks great until exporting to pdf). This solution (<a href="https://stackoverflow.com/questions/49069476/keep-formatting-when-exporting-table-with-dt-datatables-buttons-extension">Keep formatting when exporting table with DT (DataTables buttons extension)</a>) works perfectly to specifying certain formats for headers, colours, alignments etc (as I have tried in the <code>customize</code> argument). However, what I’m trying to do is to keep HTML formatting in the values, namely multiline cells (<code>&quot;A&lt;/br&gt;B&lt;/br&gt;C&quot;</code>). Does anyone know which <code>JS()</code> function I need to pass to the <code>customize</code> argument to have the <code>&lt;/br&gt;</code> be read and printed accordingly? Or is there another way to do this?</p>
  <pre class="lang-r prettyprint-override"><code>if (!require('DT')) install.packages('DT'); library(DT)
  
  dt &lt;- data.frame(Numbers = 1:100, Letter = paste0(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;), collapse = &quot;&lt;/br&gt;&quot;))
  
  datatable(dt, 
            rownames = FALSE, 
            escape = FALSE,
            extensions = c(&quot;Buttons&quot;),
            options = list(
              dom = 'Blfrtip',
              buttons = list(
                list(extend = &quot;pdf&quot;, 
                     exportOptions = list(stripHtml = FALSE,
                                          columns = ':visible'),
                     orientation = 'portrait',
                     customize = JS(&quot;function(doc){
                                    doc.styles.tableHeader.color='yellow';
                                    doc.defaultStyle.alignment = 'left';
                                    doc.styles.tableHeader.alignment = 'left';
                                    doc.pageMargins = [10,10,10,10];
                                    doc.defaultStyle.fontSize = 7;
                                    doc.styles.tableHeader.fontSize = 7;
                                    doc.styles.title.fontSize = 9;
                                    }&quot;
                                    )))))
  </code></pre>
  <p><strong>Approach 2:</strong> Save the <code>datatable</code> object to <code>html</code> using <code>saveWidget()</code>, then print the webpage using <code>webshot()</code>. Here the format is preserved, but it just makes one very long pdf, making it useless if you want to print. I've tried to find a function which would split this pdf into A4 pages: <code>webshot::resize()</code>, <code>dev.print(onefile=F)</code>, <code>staplr::split_pdf()</code>, and <code>tabulizer::split_pdf()</code>, but could not get it working.</p>
  <pre class="lang-r prettyprint-override"><code>if (!require('webshot')) install.packages('webshot'); library(webshot)
  if (!require('DT')) install.packages('DT'); library(DT)
  
  df &lt;- data.frame(No = 1:100, Letter = paste0(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;), collapse = &quot;&lt;/br&gt;&quot;))
  
  dtable &lt;- datatable(df,
                       rownames = T,
                       width = '100%',
                       height = '100%',
                       escape = FALSE,
                       options = list(
                         pageLength = 200,
                         dom = 't'
                       ))
  
  html &lt;- &quot;dtable.html&quot;
  saveWidget(dtable, html)
  webshot(html, &quot;dtable.pdf&quot;)
  </code></pre>
  <p>I feel like I'm making this harder than it should be. Am I missing something? Is there perhaps another method than the <code>DT::datatable()</code> which can export multiline cells to pdf via R?</p>