bambamfox
bambamfox

Reputation: 33

Printing from knitr/rmarkdown crops images and introduces linebreaks

I am creating a questionnaire and want to let users to save the results as pdf.

The html looks fine! But printing crops the image and nothing has worked so far.

enter image description here

Edit: I add a fitting quote about the pagedown package, probably describing the problem:

"[...] try to generate PDF from one of your Rmarkdown files, and by a PDF I mean that kind of document where images are note broken into half from page to page and written text does not finish beyond the end of the page… (yeah, I know you know what I mean bu that…)"

# YAML
---
output: 
    pdf_document:   #html?
        fig_crop: false                         # doesn't work
---

---
output:
  pagedown::html_paged:                         # doesn't work
    css: ["default-fonts", "default-page", "default"]
---

# CSS line-breaks
<style media="print">
    html, body, p, hr, img { 
    break-after:  avoid !important;             # doesn't work
    break-before: avoid !important;             # doesn't work
    break-inside: always !important;            # doesn't work
    } 
</style>

# Button
<input type="button" value="Print this page" onClick="window.print()">
# Example Plot
     ```{r echo=FALSE, message=FALSE}
    library(ggplot2)
    Plot <- ggplot(mpg, aes(displ, cty)) + geom_point()
    Plot + facet_grid(rows = vars(drv))
    plot.height <- 45
     ```


# Calling the plot (with fig.height)
    ```{r, fig.height=plot.height, strip.white = TRUE} # doesn't work
     plot(Plot)
    ```

Here is the example implemented: https://exampleruntest.formr.org For some reason the example plot does not introduce the line break before the plot, but afterwards.

I hope someone has an idea

Upvotes: 1

Views: 269

Answers (1)

bambamfox
bambamfox

Reputation: 33

Answering my question:

  1. The image cropping turned out to be a firefox specific issue. Solutions are this and this, while the latter worked for my case by adding display:block; to the CSS

  2. Removing the page-break after the header by wrapping the image of the plot with a <div> (overriding the default <p>) using this solution.

Upvotes: 1

Related Questions