How to convert asciidoc to pdf?
Sebastian Wright
I attempted to convert my mybook.adoc to mybook.pdf using pandoc, and got the following error.
$ pandoc -s mybook.adoc -t asciidoc -o mybook.pdf
pandoc: cannot produce pdf output with asciidoc writerIs there another tool that I should use instead of or in concert with pandoc?
How can I convert asciidoc to pdf?
9 Answers
Prerequisites: all that’s needed is Ruby (1.9.3 or above)
Install a native PDF converter for AsciiDoc - Asciidoctor PDF:
gem install asciidoctor-pdf --preRun:
asciidoctor-pdf mybook.adocYou should get the file mybook.pdf in the same directory.
1If you're using VS Code, Ctrl + SHFT + P and type AsciiDoc: Export document as pdf
If you have Docker installed, you can use the AsciiDoctor Docker Image. Then you can use the image interactively or in batch mode as explained below:
Prerequisites
- Install Docker
Install the Docker image:
$ docker pull asciidoctor/docker-asciidoctor
Create the pdf by executing the following command:
$ cd [directory to where my book.adoc is located]
$ docker run --rm -v $(pwd):/documents/ asciidoctor/docker-asciidoctor asciidoctor-pdf mybook.adocExplanation
docker runstarts a Docker container--rmcleanup - removes the Docker container after the command has been executed-vmount a volume to the image$(pwd)get the path to the current directory, e.g. the value of[directory to where my book.adoc is located]abovedocumentsthe name of the mounted volume in the running containerasciidoctor/docker-asciidoctorthe name of the Docker image that is used to create the Docker containerasciidoctor-pdfthe command that actually triggers the pdf generationmybook.adocthe name of the AsciiDoc source file to generate the pdf from
See also the docker run documentation
AsciiDoc has a couple of toolchains that will render PDFs from AsciiDoc source. You can use the Ruby-based AsciiDoc -> Prawn -> PDF toolchain developed by Asciidoctor, or you can use the older AsciiDoc -> DocBook -> FOP toolchain. These are rendering/publishing procedures, whereas Pandoc mainly provides for conversion and is best for converting from one o another type of source format.
I now realize the ideal route from asciidoc to pdf is to first convert the asciidoc to docbook. This employs the intended sphere of asciidoc and a docbook->pdf converter using each in its separate area of concern.
Furthermore, instead of pandoc I have found that when mathematical formulas are included, it was easier to use dblatex for docbook->pdf conversion. Thus my conversion pipeline is the following:
asciidoc -> asciidoctor -> docbook -> dblatex -> pdf
A sample make file:
make: my.adoc asciidoc -b docbook my.adoc dblatex my.xmlThis discussion page suggests that asciidoc is primarily designed to compile to docbook.
The easiest way to convert AsciiDoc document to PDF is as follows -
- Open AsciiDocLive
- Paste your document in the editor and see the preview
- save it as HTML
- Open any online HTML to PDF convertor
- Upload your newly generated HTML file
- Convert to PDF and download it.
Done.
Following @Remis's answer, I used asciidoctor's pdf backend.
I could not figure out how to install asciidoctor-pdf on nixos, but managed to find the following alternative which worked:
asciidoctor -r asciidoctor-pdf -b pdf or asciidoctor-pdf Another workflow, which I use a lot, is to create HTML from your AsciiDoc file using the AsciiDoctor processor, linking a custom CSS stylesheet with print styling rules:
asciidoctor -a stylesheet=path-to-stylesheet.css file.adocYou can then open the HTML file in your browser and print to PDF.
For more advanced styling, such as page numbering, headers, footers, page breaks and columns, you can use PrinceXML (or other similar products). This is a commandline processor that will convert the HTML file to PDF using the styling rules in the CSS used by the HTML file. PrinceXML applies styling rules that are in the CSS3 standard but which aren't yet supported by browsers, allowing you to create finely styled layouts.
In case if you have AsciiDoc plugin in Intellij, here is the way you can generate -
- create the adoc file ex - "tktds-dhs-e2e-gcp.adoc" and write your content.. or you may already have adoc file..
- At top where file is opened in editor, click on PDF or HTML button to generate .pdf or .html files {need to click 1 at a time} refer below image -
- Further you can find pdf / html file in same directory, refer below image -