Add Image (Png File) to Header of PDF File Created with R

Add image (png file) to header of pdf file created with R

here's a suggestion,

library(ggplot2)
p.example = qplot(mpg, wt, data = mtcars)

library(grid)
library(gtable)
ann <- rasterGrob(mypng, width=unit(1,"cm"), x = unit(0.5,"cm"))
g <- ggplotGrob(p.example)

g <- gtable_add_rows(g, grobHeight(ann), 0)
g <- gtable_add_grob(g, ann, t=1, l=4)
grid.newpage()
grid.draw(g)

Sample Image

Render logo.png in header of pdf output shiny - Rmarkdown

Basically you already figured out what's the issue. Hence one approach to fix your issue would be to do copy both the report template and the logo to the same temporary directory.

# Define the server code
server <- function(input, output) {
output$plot <- renderPlot({
hist(runif(input$n))
})
# create markdown report ----------------------------------
output$report <- downloadHandler(
filename = "report.pdf",
content = function(file) {
td <- tempdir()
tempReport <- file.path(td, "report.Rmd")
tempLogo <- file.path(td, "logo.png")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
file.copy("logo.png", tempLogo, overwrite = TRUE)

params <- list(scores = input$n)

rmarkdown::render(tempReport,
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}

create an PNG image of a flextble object

The following code should help:

insert_screenshot = function(x, png_file ) {
htmltools::save_html(htmltools_value(x), 'temp.html')
res = webshot::webshot('temp.html', png_file)
unlink('temp.html')
res
}
insert_screenshot(plotIn(finalResults), 'my-screenshot.png')

magick image library writing incorrect header to PDF files?

I have held off from answering this because the solution is embarrassingly obvious. In retrospect, I just assumed that, because I used image_read_pdf it should and would save in PDF format. What I needed to do was specify it explicitly. Adding a format = "pdf" argument to the image_write call achieved that.

image_write(pdfimage3, path = "C:/temp/test.pdf", density = 300, format = "pdf", flatten = TRUE)

This results in a well-formed PDF. Problem solved. Lesson learned.

PDF file with proper header

add image in title page of rmarkdown pdf

I was able to solve this using LaTeX package titling

---
title: "Untitled"
author: "Name"
date: "September 19, 2015"
output:
pdf_document:
includes:
in_header: header.tex
---

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Where the header.tex should include the following code:

\usepackage{titling}

\pretitle{%
\begin{center}
\LARGE
\includegraphics[width=4cm,height=6cm]{logo.png}\\[\bigskipamount]
}
\posttitle{\end{center}}

and replace logo.png with the image you would like to use and make sure the file is in the root directory of your Rmd file. You can change image width and height to your needs. For more information on available options go to titling

Insert a logo in upper right corner of R markdown pdf document

Ok, I have found the solution:

---
title:
header-includes:
\usepackage{graphicx}
\usepackage{fancyhdr}
\pagestyle{fancy}
\setlength\headheight{28pt}
\fancyhead[L]{\includegraphics[width=5cm]{GPIM_Logo_300x85.png}}
\fancyfoot[LE,RO]{GPIM}
output: pdf_document
---

Adding a logo image with code written in R Markdown's YAML

The following works for me, both on Windows and (similar) also on Linux. I had also problems that the files with the logos were not found, so I put html header, footer and images in an absolute path (on Windows) resp a path absolute to the Webserver document root on Linux:

---
pagetitle: My page title
lang: en
output:
html_document:
include:
before_body: /resources/header.html
after_body: /resources/footer.html
runtime: shiny
---

I have to add, that this applies to a shiny runtime.

Edit

Just tested: Without a shiny server, all parts can be in the work folder and both, include and includes work:

---
pagetitle: My page title
lang: en
output:
html_document:
include:
before_body: header.html
after_body: footer.html
---

And here an example header.html file:

<body>
<p style="background-color: #002557; height: 80px;">
<a href="https://foo.de/">
<img src="left_logo.png" alt="Left Logo"
title="Left Logo" style="width: 125px; height: 36px; margin-top: 22px; margin-left: 4%;">
</a>

<a href="https://foo.de/bar/">
<img src="right_logo.png" alt="Right Logo"
title="Right Logo" style="height: 48px; margin-top: 16px; float: right; margin-right: 4%;">
</a>
</p>
</body>

Improvements are welcome.

R markdown: can I insert a pdf to the r markdown file as an image?

Sorry, I found that there is a similar post before:
Add pdf file in Rmarkdown file

Basically, I can use something like below works well for the html output:

<img src="myFirstAlignment2.pdf" alt="some text" width="4200" height="4200">

And something like below works well for the pdf output:
(1)possible solution

\begin{center} <br>
\includegraphics[width=8in]{myFirstAlignment2.pdf} <br>
\end{center}

(2)possible solution
![Alt](myFirstAlignment2.pdf)

The myFirstAlignment2.pdf should be replaced with path\myFirstAlignment2.pdf if the pdf file is not in your working directory.

Include images for pdf and html format

Short answer: your image is a PDF File


It always helps to include a minimal, reproducible example. As the path to your image was not provided, I have created one here which creates a local file cars.pdf:

---
title: Images in Knitr
output:
html_document: default
pdf_document: default
---

```{r}
# Create an example plot
pdf("cars.pdf")
plot(cars)
dev.off()

```

```{r}
knitr::include_graphics("cars.pdf")
```

Comparing the HTML and PDF, we can see the behaviour you report, whereby the images aren't shown in the HTML:

Sample Image

The problem stems from the image being saved as a PDF. From my understanding, HTML has no native way to display such images. It therefore doesn't display the image within the output.

As a workaround, you would be better to save the graphic as a PNG or JPEG.



Related Topics



Leave a reply



Submit