Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to export slides to PDF? #168

Closed
pat-s opened this issue Sep 27, 2018 · 29 comments · Fixed by #177
Closed

Function to export slides to PDF? #168

pat-s opened this issue Sep 27, 2018 · 29 comments · Fixed by #177
Milestone

Comments

@pat-s
Copy link
Collaborator

pat-s commented Sep 27, 2018

With all the different tools and confusion going on about this topic, I am wondering if we should add a function that takes care of this.
I we can agree on one tool, its much easier for users to use this function rather than reading all the recommendations in the wiki.

I would vote for decktape which produces perfect PDF outputs (no shifts, no misalignments). I use it since xaringan exists.
There is a hosted docker version that we could use.
However, ofc the user needs to have docker installed then.
Or we could add a switch in case the user has installed decktape locally.
Additionally, the function could open the produced PDF after is has been created.

I got a function for personal use but implementing it into the package would inflict some additional work ofc.

Let me know what you think :)

@zhouyisu
Copy link

I would vote for the decktape version as well. I am using the docker version in a course I am teaching with students with a non-technical background. Students are able to get it working for their purposes. I personally feel the npm version is a bit messy to maintain for them. An integrated approach would be a nice addition.

@jvcasillas
Copy link
Contributor

+1

@tcgriffith
Copy link
Collaborator

I would love this feature as well!

Since xaringan's html is "not so" self-contained, I prefer a pdf from time to time.

@garthtarr
Copy link

I've just started using the decktape method which works well:

system("`npm bin`/decktape remark file.html file.pdf")

It would be great if this was built in somehow.

Side question: does anyone know how to make decktape use the print CSS class? Perhaps @pat-s @zhouyisu knows? This would let me fully replace manually opening the HTML file in Chrome and printing to pdf.

@ekstroem
Copy link
Collaborator

ekstroem commented Oct 6, 2018

I also use decktape to circumvent the glitches that can occur when printing to pdf from the browser. I've decktape installed on the computer and use the following script to convert hmtl to pdf from the command line. Just in case anyone else could use it.

#!/bin/bash
FILES="$@"
for i in $FILES
do
    echo "Processing xaringan slide deck $i ..."
    
    filename=$(basename "$i")
    extension="${filename##*.}"
    filename="${filename%.*}"

    decktape remark $i $filename.pdf
done

@pat-s
Copy link
Collaborator Author

pat-s commented Oct 7, 2018

I started yesterday but it seems a bit complicated. While everything works when using the terminal, all R integrations of docker (stevedock, harbor) or simply the system2() command throw weird errors.

So I went with the simple system() command which worked fine.
Next, docker is highly OS sensitive, so configuration differs a lot between OS and even among the Linux distributions. So it might be kinda hard to catch all errors messages in the first place and provide solutions.

I think documentation is the key here. Stay tuned!

@yihui
Copy link
Owner

yihui commented Oct 31, 2018

If you have a recent version of Google Chrome or Chromium installed, you may try the pagedown package:

remotes::install_github('rstudio/pagedown')
pagedown::chrome_print('any-file.html')

From my quick testing, MathJax and HTML widgets don't seem to work well.

@yihui yihui closed this as completed in #177 Nov 9, 2018
yihui pushed a commit that referenced this issue Nov 9, 2018
* add PBSmodelling to suggests, tidy

* glue = suggests

* NS

* add export_pdf

* add test

* update .Rproj

* re-add license

* glue -> sprintf
PBSmodelling -> animation

* update doc

* use roxygen markdown, revert to old DESC structure

* delete test

* Update xaringan.Rproj

* install libmagick on travis

* import auto_browse from animation pkg

* no defaults for xaringan_path and pdf_path

* only one system() call to docker

* shorten SO link

* remove magick dep from travis

* export_pdf -> decktape()

* revert roxygen markdown change

* update man & NS

* remove animation from suggests

* fix auto_browse()

* also support the local decktape library if it has been installed, so that users don't have to install Docker

* add a news item for #177

* note that file could be a URL when not using Docker
@yihui
Copy link
Owner

yihui commented Nov 9, 2018

A xaringan::decktape() function has been added thanks to @pat-s. You can use either Docker or just the decktape command directly if installed. Please test the function with remotes::install_github('yihui/xaringan'). Thanks!

@yihui yihui added this to the v0.8 milestone Nov 9, 2018
@sje30
Copy link
Contributor

sje30 commented Dec 3, 2018

hi Yihui,
thanks for this. Unfortunately for me decktape doesn't work:

> decktape('rmd-intro.html', 'o.pdf')
Loading page file:///Users/stephen/papers/talks/2018/2018-12-07-rmd/rmd-intro.html ...
Loading page finished with status: 0
Remark JS plugin activated

Error: Evaluation failed: DOMException: Failed to read the 'rules' property from 'CSSStyleSheet': Cannot access rules
    at _ (__puppeteer_evaluation_script__:4:20)
Error in decktape("rmd-intro.html", "o.pdf") : 
  Failed to convert rmd-intro.html to PDF

This is documented https://github.com/astefanutti/decktape (2nd error) and passing the extra args from the command line works(*) for me

`npm bin`/decktape   --chrome-arg=--allow-file-access-from-files file.html file.pdf

(*) I say works, but not quite. The italics text in my slides are missing -- I will come up with a minimal example for this.

@yihui
Copy link
Owner

yihui commented Dec 3, 2018

@sje30 I just added the argument args which defaults to --chrome-arg=--allow-file-access-from-files in decktape(). I'm not a DeckTape or Docker expert, so @pat-s please please verify if I did it correctly. Thanks!

@pat-s
Copy link
Collaborator Author

pat-s commented Dec 3, 2018

AFAIK the local decktape installation is not very robust across OS since it needs a working combination of JS libraries and the respective chromium version (and possibly has more complicated interactions I do not know of).

For example, on Arch I usually need the arg --no-sandbox to get it working. But this is totally OS dependent.

If the default arg added does no harm to other users (we'll see) we can keep it. But I would advise against adding more and more default args that may help other people in the future.

In general, I would recommend the docker version of decktape if people face errors with the local installation. However, last time I checked the last 5 docker versions were also broken so it is always a bit of a "guessing game".
I use the hosted docker v2.8.0 since several months without problems.

@tcgriffith
Copy link
Collaborator

Do we have other options besides decktape? the dependencies seem to be too heavy for a simple feature like "exporting to pdf"

@yihui
Copy link
Owner

yihui commented Dec 4, 2018

I wrote a simple wrapper function pagedown::chrome_print() to print arbitrary HTML pages to PDF via Chrome, but currently it doesn't work well for pages containing HTML widgets or MathJax:

remotes::install_github('rstudio/pagedown')
pagedown::chrome_print('https://slides.yihui.name/xaringan/')

Exporting to PDF sounds like a simple feature, but it can be extremely complicated. The complication comes from the fact that it is hard to know when a page is ready to be printed when the page contains JavaScript code, because JavaScript can modify the DOM, and the browser has to wait until the DOM is stable. Decktape must have used certain remark.js events to know when the page has been fully processed by remark.js.

@sje30
Copy link
Contributor

sje30 commented Dec 4, 2018

Thanks - just trying pagedown. It mostly works:

pagedown::chrome_print('https://sje30.github.io/talks/2018/rmd-intro.html')

does pretty well, but the italics (e.g. on slide 7) and mathjax are missing from the pdf. This is on mac, viewing the pdf in preview:

screen shot 2018-12-04 at 10 10 00

happy to help test/debug. source of slides are at: https://github.com/sje30/2018-12-07-rmd

@yihui
Copy link
Owner

yihui commented Dec 14, 2018

@sje30 I'm not surprised by the result. We need to wait for rstudio/pagedown#30 to be merged for a more stable pagedown::chrome_print().

@yihui
Copy link
Owner

yihui commented Feb 7, 2019

remotes::install_github(c('rstudio/pagedown', 'yihui/xaringan'))

Note that at this moment, the PDF is created asynchronously, and it may surprise you that the PDF file comes a few seconds later after chrome_print() quits. I'm still working on this issue. Other that, pagedown::chrome_print() should work reasonably well now.

@sje30
Copy link
Contributor

sje30 commented Feb 8, 2019

Thanks; see #193 for a minor typo though (DESCRIPTION has version "0.8." rather than presumably 0.8

@yihui
Copy link
Owner

yihui commented Feb 8, 2019

Fixed now. Thanks! BTW, the issue with pagedown::chrome_print() as I mentioned above has also been fixed.

@sje30
Copy link
Contributor

sje30 commented Feb 8, 2019

Thanks. Does it now fix the italics issue I raised? (See above). It doesn't for me when I do this:

pagedown::chrome_print('https://sje30.github.io/talks/2018/rmd-intro.html')

@garthtarr
Copy link

garthtarr commented Feb 8, 2019 via email

@sje30
Copy link
Contributor

sje30 commented Feb 8, 2019

Thanks, yes, I did an install of pagedown and xaringan. does the italics work for you? The only change I made is on my laptop, I have to add browser="google-chrome-stable" to the call to chrome_print.

@sje30
Copy link
Contributor

sje30 commented Feb 8, 2019

sorry, I misunderstood. No, I haven't remade the .html file. Let me try that now!

@yihui
Copy link
Owner

yihui commented Feb 8, 2019

It seems you have regenerated the HTML page: sje30/sje30.github.io@e6f3764 I just tested it. The italic text and MathJax expression are no longer missing:

image

@sje30
Copy link
Contributor

sje30 commented Feb 8, 2019

Thanks for testing it! Yes, I was just in the process of updating and telling you. Thanks for taking the time on this. I appreciate it.

@MadNietzsche
Copy link

Hello,
I was just trying this new fix and it prints well except for the plots. I get the following error message:

pagedown::chrome_print("C:/Users/aaalvarez/Documents/cox/pres_1.html")

[2019-02-12 17:49:47] [error] handle_read_frame error: asio.system:10054 (An existing connection was forcibly closed by the remote host.)
[2019-02-12 17:49:47] [info] asio async_write error: asio.system:10054 (An existing connection was forcibly closed by the remote host.)
[2019-02-12 17:49:47] [fatal] handle_write_frame error: websocketpp.transport:2 (Underlying Transport Error)

Does anyone know what this could be? I'll appreciate any comment

@yihui
Copy link
Owner

yihui commented Feb 12, 2019

@MadNietzsche Please see rstudio/pagedown#68.

@afranks86
Copy link

Thanks for this feature-- I annotate slides in class on my ipad so the pdf conversion is very useful! I was wondering if incremental bullet points are supported in any of the pdf conversion tools? It would be awesome if incremental bullets were split across slides when converting to pdf (i.e. the way beamer handles incremental bullets).

@yihui
Copy link
Owner

yihui commented Oct 2, 2019

@afranks86 It is possible, but please file a new issue or ask questions in public forums instead of replying to a closed issue. It feels stressful when an issue can never been truly closed...

@jhelvy
Copy link

jhelvy commented Feb 14, 2021

FYI, I've been working on a package that builds xaringan slides to multiple output types: html, pdf, pptx, gif, and a couple options for a png of the first slide:
https://github.com/jhelvy/xaringanBuilder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.