Skip to content

Generate PDF using Chrome web browser

Maxim Kim edited this page Aug 12, 2022 · 1 revision
https://img.youtube.com/vi/AH31UvVftqE/0.jpg
  1. HTML is generated using :make;
  2. then headless chrome web browser is used to generate PDF out of HTML.

Add to ~/.vim/after/ftplugin/rst.vim:

" Silently execute OS command
func! s:OSexe(cmd) abort
    if exists("$WSLENV")
        lcd /mnt/c
        let runner = ":silent !cmd.exe /C start"
    elseif has("win32") || has("win32unix")
        let runner = ':silent !start'
    elseif executable('xdg-open')
        let runner = ":silent !xdg-open"
    elseif executable('open')
        let runner = ":silent !open"
    else
        echohl Error
        echomsg "Can't find an executor for a command!"
        echohl None
        return
    endif
    try
        exe runner . ' ' . a:cmd
    catch
        echohl Error
        echomsg v:exception
        echohl None
    finally
        if exists("$WSLENV") | lcd - | endif
        redraw!
    endtry
endfunc

" NOTE: change chrome executable to what you have in your system.
command -buffer Rst2Pdf make | exe s:OSexe(printf('"%s" %s %s "%s"',
      \ 'C:/Program Files/Google/Chrome/Application/chrome.exe',
      \ '--headless --disable-gpu --print-to-pdf-no-header',
      \ '--print-to-pdf="' . expand("%:p:r") . '.pdf"',
      \ expand("%:p:r") . '.html'
      \ ))

Note

The PDF would be similar to HTML version, so your CSS matters.

Clone this wiki locally