rmarkdown-cookbook icon indicating copy to clipboard operation
rmarkdown-cookbook copied to clipboard

How to add a cover image in a PDF output document

Open cderv opened this issue 3 years ago • 9 comments

cover image works for epub document. For PDF document, some command must be passed between \begin{document} and \maketitle - this is not supported in the Pandoc template by default.

One solution is here : https://github.com/rstudio/bookdown/issues/473#issuecomment-672718875, using the titlepic tex package.

Could be a recipe for a second edition of the book (or maybe a new blog called "The rmarkdown cookblog" 😉 and the second edition would be a compilation of the post in the blog 😄 )

cderv avatar Aug 12 '20 08:08 cderv

The solution linked above puts the cover after the title page, which is not ideal in my opinion. I believe it should come first.

jtbayly avatar Sep 21 '20 18:09 jtbayly

If you have another recipe to share for that, do not hesitate to share it (or the link) here so that we'll look into it when working on second edition. Thank you !

cderv avatar Sep 22 '20 07:09 cderv

The following works and allows either a PDF or a regular image to be used as the cover.

header-includes: 
- \usepackage{titling}
- \usepackage{pdfpages}
- \pretitle{\begin{center}\includepdf{cover.jpg}}
- \posttitle{\end{center}}

However, it also inserts a blank page before the cover page in my tests. The following gets rid of the blank page, but it might have side effects. I don't really understand it.

header-includes: 
- \usepackage{titling}
- \usepackage{pdfpages}
- \pretitle{\begin{center}\includepdf{cover.jpg}}
- \posttitle{\end{center}}
- \usepackage{atbegshi}% http://ctan.org/pkg/atbegshi
- \AtBeginDocument{\AtBeginShipoutNext{\AtBeginShipoutDiscard}}

Perhaps somebody can figure out why there's a blank page in the first place and get rid of it the correct way?

jtbayly avatar Sep 22 '20 16:09 jtbayly

Thanks a lot for sharing !

cderv avatar Sep 22 '20 17:09 cderv

From @aldomann

One minor issue with using titling is that (at least on my tests with bookdown::pdf_document2) is that it replaces the subtitle specified on the YAML header. EDIT: actually it seems to redefine the entire title section (font sizes, spacing, etc), as seen here.

cderv avatar Jan 04 '21 13:01 cderv

EDIT: this unfortunately strips the metadata / breaks internal links, so strike that idea!

this is something i've hit on recently, as well, and after trying to implement something and integrate it into an existing cls file for a bit, i realized i was going about this in one way that might not be the easiest to accomplish.

so!

instead, i've decided to approach this problem by rendering the book and then combining the pdf with the cover image i'm using qpdf::pdf_combine, which doesn't like to overwrite a file as far as I can tell (i get blank pages), so it takes an intermediate step, but it works!

bookdown::render_book(input = "index.Rmd", output_format = "bookdown::pdf_book")
qpdf::pdf_combine(input = c("img/cover.pdf", "_book/_book.pdf"), output = "_book/_book2.pdf")
file.rename(from = "_book/_book2.pdf", to = "_book/_book.pdf")

juniperlsimonis avatar Jul 04 '21 22:07 juniperlsimonis

Won't this step overwrite the PDF metadata?

aldomann avatar Jul 06 '21 21:07 aldomann

Won't this step overwrite the PDF metadata?

unfortunately, yes :( back to the drawing board

juniperlsimonis avatar Jul 07 '21 00:07 juniperlsimonis

Anyone found any decent solution to this?

vikalpjain avatar Mar 30 '23 16:03 vikalpjain