• Stars
    star
    111
  • Rank 303,753 (Top 7 %)
  • Language
    Java
  • License
    MIT License
  • Created almost 11 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A portable DocBook-to-PDF build command that wraps DocBook XSL and Apache FOP

asciidoctor-fopub

DocBook-to-PDF conversion using free software made easy! (based on DocBook XSL and Apache FOP)

Using the asciidoctor-fopub project, you can convert any DocBook file into a nicely formatted PDF with nothing more than a Java runtime (JVM) and development kit (JDK). All the open source software required to perform the conversion is automatically fetched from the internet the first time you run it.

Scope
fopub is a replacement for a2x command from AsciiDoc Python. a2x is a standalone frontend to the DocBook toolchain. fopub (like a2x) is meant to be used when you don’t have a Gradle (or Maven) build. Once you are in the Gradle world, you can simply leverage the jDocBook plugin to accomplish what fopub is doing (as seen in the Spring and Hibernate builds).

Doing is believing

Before we get into the goals and the technical details of the project, let’s see the conversion in action!

Prerequisites

The only prerequisite to perform the DocBook to PDF conversion is a Java Development Kit (JDK), which includes the Java runtime (and, naturally, an internet connection). You can use any recent JDK (i.e., Java SE 8 or better).

If Java runtime (JVM) is setup correctly, you should be able to type:

$ java -version

or

$ "$JAVA_HOME/bin/java -version"

and see output that looks like:

openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b15)
OpenJDK 64-Bit Server VM (build 25.181-b15, mixed mode)

If the Java Devlopment Kit (JDK) is setup correctly, you should be able to type:

$ javac -version

or

$ "$JAVA_HOME"/bin/javac -version"

and see output that looks like:

javac 1.8.0_181

If not, you can install a JDK using your system’s package manager or by downloading a distribution from the Java for developers download site.

Warning
You may encounter problems if you attempt to use the GNU compiler for Java (gcj). We recommend using OpenJDK or Oracle Java.
Tip
It’s not even necessary to install the JDK on your system. Simply set the JAVA_HOME environment variable to the location where you extract the distribution.

Next, you need to retrieve the asciidoctor-fopub project.

Retrieve the project

You can retrieve the asciidoctor-fopub project in one of two ways:

  1. Clone the git repository

  2. Download a zip archive of the repository

Option 1: Fetch using git clone

If you want to clone the git repository, simply copy the URL of the repository on GitHub and pass it to git clone command:

$ git clone https://github.com/asciidoctor/asciidoctor-fopub

Next, change to the project directory:

$ cd asciidoctor-fopub

Option 2: Download the archive

If you want to download a zip archive, click on the Download Zip button on the right-hand side of the repository page on GitHub. Once the download finishes, extract the archive, open a console and change to that directory.

Tip
Instead of working out of the asciidoctor-fopub directory, you can simply add the directory to your PATH environment variable.

Next, let’s grab a file to convert.

Generate a DocBook file

If you don’t already have a DocBook file, you can generate one using Asciidoctor (or your tool of choice).

To create a DocBook file using Asciidoctor, first create an AsciiDoc file named sample.adoc in the current directory and populate it with the sample content below.

sample.adoc
= Document Title
Doc Writer <doc@example.com>

A sample http://asciidoc.org[AsciiDoc] document.

== Introduction

A paragraph followed by a simple list.

* item 1
* item 2

Here's how you say "`Hello, World!`" in Ruby.

.A basic Ruby application
[source,ruby]
----
puts "Hello, World!"
----

TIP: asciidoctor-fopub takes the pain out of converting a DocBook file to a PDF file like this one.
All you need is a Java Development Kit (JDK) and this project.
The rest of the software is fetched and configured by Gradle.

Convert the AsciiDoc file to DocBook using the asciidoctor (or asciidoc) command:

$ asciidoctor -b docbook -d book -a data-uri! sample.adoc

After executing this command, you should now see a new file named sample.xml in the current directory.

Important
We explicitly disable the data-uri attribute just in case it’s set in the AsciiDoc document. The PDF processor will choke if it comes across embedded image data in the generated DocBook.

It’s time to convert it to PDF!

Tip
You could also try these steps using the README.adoc file in the root directory of the project.

Convert DocBook to PDF

We’re now ready to do the conversion! It’s as simple as running the fopub script in the current directory on our DocBook file.

On Unix-based systems (e.g., Linux, OSX), run:

$ ./fopub sample.xml
Important
Since we’re executing a local script, you need to prefix the name of the command with ./.
Tip
If you’ve added the path to asciidoctor-fopub to your PATH environment variable, you can leave off the ./ and execute it from any directory.

On Windows, run:

$ fopub sample.xml
Note
The first time you run the command, asciidoctor-fopub has to retrieve the software from the repositories and setup the conversion application, so be patient.
Tip
To use the fopub command from behind a proxy, you’ll need to configure the Gradle proxy settings as described in Chapter 20 of the Gradle Reference Guide.

When it’s all said and done, you should now see the file sample.pdf in the current directory. Open that file with a PDF viewer to see the result.

Screenshot of sample PDF document
Figure 1. Sample PDF document rendered in viewer

As you can see, all the details of the conversion are hidden behind the scenes. You get to focus on getting the job done, not worry about the mess that has to be sorted out to use Apache FOP correctly and get a decent-looking document.

Custom XSL parameters

Any arguments that follow the source file name are passed directly to the Apache FO processor (fop). This feature allows you to assign XSL parameters, among other things.

Let’s say you want to set the orientation of the PDF to landscape. The DocBook XSL templates recognize the parameter named page.orientation. Here’s how you would pass that through fopub to fop.

$ ./fopub sample.xml -param page.orientation landscape

You’ll now notice that the PDF generated is rendered in landscape mode.

Note
See DocBook XSL parameter reference for a list of all XSL parameters you can set.

Custom XSL templates

When you work on many documentations projets in parallel, you will probably need different outputs.

Use cases for different templates
structure
  1. One needs a picture in the book title page

  2. The other needs a special text at the bottom of this page

style
  1. One needs the default Asciidoctor style

  2. The other uses the Colony style

In order to work on different documentation projects in parallel, you need to have different docbook-xsl directories.

How it works:

  1. Copy the docbook-xsl directory from the fopub install directory to your documentation path, /path/to/custom/fopub.

    Content of docbook-xsl directory
    Figure 2. Content of the docbook-xsl directory
  2. Update the files you want in this directory

    For example if you want to use Colony style

    You delete Asciidoctor theme, Foundation theme and you uncomment Colony theme in the common.xsl file.

  3. Then you specify this directory when you launch the output generation :

    $ /path/to/fopub/fopub -t /path/to/custom/fopub/docbook-xsl sample.xml

fopub option flags

-t <path>

tells fopub which docbook-xsl directory to use (optional, defaults to location inside fopub installation)

-f [pdf|ps|fo|xhtml]

tells fopub which output to produce (optional, defaults to pdf)

-h

prints usage

Motivation

The asciidoctor-fopub project aims to provide a simple mechanism for converting DocBook to PDF. The plan is to use some form of this project to handle the DocBook to PDF conversion in Asciidoctor. We hope it’s generally useful outside of Asciidoctor as well.

If you’ve ever had to do this conversion, you will appreciate how overly-complex it is. It requires fetching the right combination of software (including the right versions), putting all the files in the right location and associating them together using a catalog and passing in the correct parameters. It’s boring and tedious. This project handles all that magic so you don’t have to.

In addition to making the conversion work, the project includes the following features that are often left out:

  • Works with DocBook generated by AsciiDoc (supports all AsciiDoc processing instructions)

  • Syntax highlights source code listings using XSLTHL (including a highlighter for AsciiDoc source)

  • Scales down images to fit within the width of the page

  • Applies (configurable) formatting and styling that’s consistent with the Asciidoctor themes

  • Loads and embeds system fonts necessary to support the themes (Arial, Georgia and Liberation Mono)

  • Applies configuration to embed SVG-based admonition icons and callout marks

  • Works without an internet connection (once the initial run is complete); drastically speeds up execution

  • Works from any directory (planned)

There’s a lot of research that went into making all that happen for you :)

Note
One of the most important features of this tool—​and one of the most difficult to get right—​is that it works offline. By default, XSL processors fetch all necessary resources from the internet. Since these files are large and reference many other files, fetching them from the internet is exceptionally slow and a waste of network bandwidth. The fopub tool carefully ensures that the processor has all the files it needs (on the first run) and thus keeps it from reaching out to the internet while it performs the conversion.

Technical details

Let’s talk tech.

The conversion’s key players: Apache FOP and DocBook XSL

The main goal of this project is to download, configure and execute Apache FOP to handle the conversion from DocBook to PDF using the DocBook XSL stylesheets. You can see from the first part of the DocBook XSL book what a complex proposition this is.

DocBook XSL

The purpose of DocBook XSL is to provide a standard set of XML transformations (XSLT) from DocBook to several presentational formats, one of which is XSL-FO.

Apache FOP

Apache FOP (Formatting Objects Processor) is a print formatter driven by XSL formatting objects (XSL-FO) and an output independent formatter. It is a Java application that reads a formatting object (FO) tree and renders the resulting pages to a specified output. The primary output target is PDF.

Apache FOP also includes an XSLT processor (Xalan) that handles the conversion from DocBook into the intermediatory XSL-FO format that the print formatter uses to create the PDF.

When the software is all setup, we are ultimately running a command in the fopub script similar to this one:

$ fop -xml sample.xml -xsl docbook-xsl/fo-pdf.xsl -pdf sample.pdf

In reality, it’s more complex. You can see the full command at the bottom of the fopub script.

Source highlighting using XSLTHL

Readers have come to expect source code to be highlighted so it looks the same way in the document as it does in their source code editors. XSLTHL provides source highlighting for PDF output.

XSLTHL integration is a well-hidden feature in the DocBook XSL project. It requires a Java-based XSLT processor (like the one embedded in Apache FOP) to use it. Activating it requires passing special parameters to the processor that specify the location of the configuration file and a flag to turn it on.

When everything falls into place, as it does with the fopub script, you get nice syntax highlighting in your PDF file and happy readers.

MathML rendering using JEuclid

If you have MathML in your DocBook, it will be rendered using the JEuclid plugin for Apache FOP.

Asciidoctor automatically converts AsciiMath to MathML when the asciimath gem is available.

First, install the asciimath gem.

$ gem install asciimath

Then, enable the stem attribute when converting to DocBook using Asciidoctor.

$ asciidoctor -b docbook -a stem sample.adoc

Finally, run fopub on the DocBook file as documented above.

Warning
Asciidoctor 1.5.3 only converts AsciiMath blocks. Asciidoctor 1.5.4 will also convert inline AsciiMath elements. See asciidoctor/asciidoctor#1622.
Warning
At this time, conversion of LaTeX to MathML is not supported. However, if you have a DocBook file that contains MathML, asciidoctor-fopub will convert it.
Tip
The font used for the rendered math is selected by the pickfont-math template.

Priming the pump with Gradle

Setting up a Java application is no small feat. So what fetches the software and puts it all in the right place? That handywork is performed by Gradle.

Gradle is a Java-based automation and build tool that specializes in setting up Java applications (among other capabilities). Gradle can fetch files from remote repositories, move them around, create start scripts and assemble an application distribution.

We are using Gradle to prepare a Java application into the build/fopub directory that the fopub script can execute.

You may be wondering why Gradle isn’t a prerequisite of this project. The answer is that the Gradle project provides a tool that can bootstrap Gradle from nothing. That tool, gradlew, is included with the project. It gets invoked the first time you run the fopub script. Magic.

And that’s essentially what this project is all about, magic. Converting from DocBook to PDF shouldn’t be hard. We are doing our best to hide those details so that it’s as simple as it should be.

Software versions

Software Project Version

Apache FOP

2.1

DocBook XSL

1.79.1

Apache Commons XML Resolver

1.2

Xalan

2.6.0

JEuclid

3.1.9

XSLTHL

2.1.0

Gradle

4.9

Contributing

In the spirit of free software, everyone is encouraged to help improve this project.

To contribute code, simply fork the project on GitHub, hack away and send a pull request with your proposed changes.

Feel free to use the issue tracker or Asciidoctor mailing list to provide feedback or suggestions in other ways.

Authors

asciidoctor-fopub was written by Dan Allen. It builds on prior work done by authors of the AsciiDoc, jDocBook and PressGang projects.

Copyright © 2013-2018 Dan Allen. Free use of this software is granted under the terms of the MIT License.

See the LICENSE file for details.

More Repositories

1

asciidoctor

💎 A fast, open source text processor and publishing toolchain, written in Ruby, for converting AsciiDoc content to HTML 5, DocBook 5, and other formats.
Ruby
4,437
star
2

asciidoctor-pdf

📃 Asciidoctor PDF: A native PDF converter for AsciiDoc based on Asciidoctor and Prawn, written entirely in Ruby.
Ruby
1,089
star
3

asciidoctor.js

📜 A JavaScript port of Asciidoctor, a modern implementation of AsciiDoc
JavaScript
664
star
4

asciidoctorj

☕ Java bindings for Asciidoctor. Asciidoctor on the JVM!
Java
593
star
5

asciidoctor-diagram

↔️ Asciidoctor diagram extension, with support for AsciiToSVG, BlockDiag (BlockDiag, SeqDiag, ActDiag, NwDiag), Ditaa, Erd, GraphViz, Mermaid, Msc, PlantUML, Shaape, SvgBob, Syntrax, UMLet, Vega, Vega-Lite and WaveDrom.
Ruby
419
star
6

asciidoctor-intellij-plugin

AsciiDoc plugin for products on the IntelliJ platform (IDEA, RubyMine, etc)
Java
324
star
7

asciidoctor.org

🌐 Asciidoctor project site. Composed in AsciiDoc. Baked with Awestruct.
SCSS
308
star
8

asciidoctor-maven-plugin

A Maven plugin that uses Asciidoctor via JRuby to process AsciiDoc source files within the project.
Java
298
star
9

jekyll-asciidoc

💉 A Jekyll plugin that converts AsciiDoc source files in your site to HTML pages using Asciidoctor.
Ruby
298
star
10

docker-asciidoctor

🚢 A Docker image for using the Asciidoctor toolchain to process AsciiDoc content
Shell
294
star
11

asciidoctor-vscode

AsciiDoc support for Visual Studio Code using Asciidoctor
TypeScript
281
star
12

asciidoctor-gradle-plugin

A Gradle plugin that uses Asciidoctor via JRuby to process AsciiDoc source files within the project.
Groovy
272
star
13

asciidoctor-reveal.js

🔮 A reveal.js converter for Asciidoctor and Asciidoctor.js. Write your slides in AsciiDoc!
HTML
270
star
14

asciidoctor-epub3

📘 Asciidoctor EPUB3 is a set of Asciidoctor extensions for converting AsciiDoc to EPUB3 & KF8/MOBI
Ruby
203
star
15

asciidoctor-browser-extension

⚪ An extension for web browsers that converts AsciiDoc files to HTML using Asciidoctor.js.
CSS
202
star
16

asciidoctor-maven-examples

A collection of example projects that demonstrates how to use the Asciidoctor Maven plugin.
Java
187
star
17

asciidoctor-stylesheet-factory

!DEPRECATED! This was the utility project for producing the default stylesheet for the HTML converter in Asciidoctor. The source of the default stylesheet now lives in the main Asciidoctor repository.
SCSS
178
star
18

kramdown-asciidoc

A kramdown extension for converting Markdown documents to AsciiDoc.
Ruby
174
star
19

asciidoctor-gradle-examples

A collection of example projects that demonstrates how to use the Asciidoctor Gradle plugin
Java
145
star
20

atom-asciidoc-preview

⚛ AsciiDoc preview for the Atom editor.
CoffeeScript
142
star
21

asciidoctor-kroki

Asciidoctor.js extension to convert diagrams to images using Kroki!
JavaScript
124
star
22

asciidoclet

📋 A Javadoc Doclet based on Asciidoctor that lets you write Javadoc in the AsciiDoc syntax.
Java
120
star
23

asciidoctor-latex

📐 Add LaTeX features to AsciiDoc & convert AsciiDoc to LaTeX
Ruby
106
star
24

jekyll-asciidoc-quickstart

A template project for creating AsciiDoc-based websites using Jekyll.
CSS
105
star
25

asciidoctor-extensions-lab

A lab for testing and demonstrating Asciidoctor extensions. Please do not use this code in production. If you want to use one of these extensions in your application, create a new project, import the code, and distribute it as a RubyGem. You can then request to make it a top-level project under the Asciidoctor organization.
Ruby
97
star
26

asciidoctor-confluence

Push Asciidoctor file to Confluence
Ruby
80
star
27

asciidoctor-backends

Backends (i.e., templates) for Asciidoctor, a Ruby port of AsciiDoc.
HTML
66
star
28

asciidoctor-bibtex

Add bibtex citation support for asciidoc documents
Ruby
59
star
29

docgist

Render AsciiDoc documents from Gists, GitHub, DropBox and other remote sources in the browser.
CSS
56
star
30

asciidoc-docs

The source files in this repository served as the initial contribution for the AsciiDoc Language specification project at Eclipse. This repository is now archived.
55
star
31

brackets-asciidoc-preview

Live Preview of AsciiDoc for Adobe Brackets
JavaScript
51
star
32

asciidoctor-bespoke

🅱️ An Asciidoctor converter that generates the HTML component of a Bespoke.js presentation from AsciiDoc.
Slim
49
star
33

sublimetext-asciidoc

AsciiDoc Package for SublimeText 3
Python
49
star
34

asciidoctor-mathematical

An extension for Asciidoctor that converts the content of STEM blocks and inline macros using Mathematical.
Ruby
44
star
35

atom-language-asciidoc

⚛ AsciiDoc language package for the Atom editor.
CoffeeScript
42
star
36

asciidoctorj-pdf

AsciidoctorJ PDF bundles the Asciidoctor PDF RubyGem (asciidoctor-pdf) so it can be loaded into the JVM using JRuby.
Java
33
star
37

asciidoctorj-screenshot

A set of AsciidoctorJ extensions for adding automated screenshots to an AsciiDoc document.
Groovy
32
star
38

asciidoctor-firefox-addon

🐺 An add-on for Mozilla Firefox that converts AsciiDoc files to HTML directly in the browser using Asciidoctor.js.
JavaScript
32
star
39

asciidoctor-tabs

An extension for Asciidoctor that adds a tabs block to the AsciiDoc syntax.
Ruby
29
star
40

asciidoctor-lein-plugin

A Leiningen plugin for generating documentation using Asciidoctor
Clojure
26
star
41

asciidoctor-chart

A set of Asciidoctor extensions that add a chart block and block macro to AsciiDoc for including charts in your AsciiDoc document.
Ruby
25
star
42

asciidoctor-reducer

⚗️ A tool to generate a single AsciiDoc document by expanding all the include directives reachable from the parent document.
Ruby
24
star
43

asciimath

Asciimath parser
Ruby
23
star
44

docbookrx

(An early version of) a DocBook to AsciiDoc converter written in Ruby.
Ruby
22
star
45

gitbucket-asciidoctor-plugin

A GitBucket plug-in that provided AsciiDoc rendering capabilities
Scala
18
star
46

asciidoctor-leanpub-converter

A backend for AsciidoctorJ to generate Leanpub-flavoured Markdown
Groovy
16
star
47

codemirror-asciidoc

AsciiDoc mode for CodeMirror
JavaScript
16
star
48

asciidoc-grammar-prototype

⛔ ARCHIVED: An experiment to create of a formal grammar for the AsciiDoc markup language. Work is being continued at https://github.com/Mogztter/asciidoctor-inline-parser.
Java
16
star
49

asciidoctor-cli.js

The Command Line Interface (CLI) for Asciidoctor.js
JavaScript
15
star
50

asciidoctor-documentation-planning

⛔ ARCHIVED: Planning for the documentation that covers the AsciiDoc syntax, the Asciidoctor processor, and various projects in the Asciidoctor ecosystem.
15
star
51

asciidoctorj-groovy-dsl

A Groovy DSL that allows for easy definition of Asciidoctor extensions
Groovy
14
star
52

gulp-asciidoctor

gulp-asciidoctor
JavaScript
14
star
53

asciidoctor-docs-ui

The project that produces the UI (theme & user interactions) for docs.asciidoctor.org.
CSS
12
star
54

docs.asciidoctor.org

The Antora playbook project (i.e., site manifest) for the Asciidoctor documentation site.
JavaScript
11
star
55

asciidoctor-deck.js

⛔ ARCHIVED: deck.js converter templates for Asciidoctor, implemented in Haml
HTML
11
star
56

asciidoctor-doctest

🔨 Test suite for Asciidoctor backends
Ruby
10
star
57

asciidoctorj-reveal.js

AsciidoctorJ Reveal.js bundles the Asciidoctor Reveal.js RubyGem (asciidoctor-revealjs) so it can be loaded into the JVM using JRuby
Java
10
star
58

asciidoctor-ant

🐜 Ant task to render Asciidoc documentation
Ruby
9
star
59

asciidoctor-diagram-java

Java
9
star
60

asciidoctorj-diagram

AsciidoctorJ Diagram bundles the Asciidoctor Diagram RubyGem (asciidoctor-diagram) so it can be loaded into the JVM using JRuby.
Java
9
star
61

atom-asciidoc-image-helper

⚛ Tool to make insertion of images into AsciiDocs easier in the Atom editor.
CoffeeScript
9
star
62

atom-asciidoc-assistant

⚛ AsciiDoc Assistant package for the Atom editor.
Shell
9
star
63

asciidoctor-chrome-editor

⛔ ARCHIVED: AsciiDoc Editor inside Chrome!
JavaScript
8
star
64

guard-asciidoc

Guard::AsciiDoc monitors and automatically renders your AsciiDoc documents using Asciidoctor
Ruby
8
star
65

atom-autocomplete-asciidoc

⚛ AsciiDoc language autocompletions
CoffeeScript
7
star
66

asciidoctor-docbook.js

DocBook converter for Asciidoctor.js
JavaScript
7
star
67

asciidoctor-mallard

A Mallard 1.0 converter for Asciidoctor
Ruby
6
star
68

asciidoctor-lazybones

Lazybones templates for Asciidoctor projects
Groovy
5
star
69

asciidoctor-fb2

📕 Asciidoctor FB2 is an Asciidoctor extension for converting AsciiDoc to FB2
Ruby
5
star
70

html-pipeline-asciidoc_filter

⛔ ARCHIVED: An AsciiDoc processing filter for html-pipeline based on Asciidoctor.
Ruby
5
star
71

brand

Brand assets and visual identity for the Asciidoctor project
Shell
4
star
72

opal-node-runtime

⚡️ Opal Runtime specifically designed for Asciidoctor
JavaScript
4
star
73

asciidoctor-template.js

⛔️ DEPRECATED: Generic template backend for Asciidoctor.js
JavaScript
4
star
74

asciidoctor-deb-mirror

A mirror of the asciidoctor deb (Debian) package build in the pkg-ruby-extras package group. DO NOT PUSH CHANGES TO THIS MIRROR.
Ruby
3
star
75

asciidoctor-community-docs

Process, policy, and other shared documentation for the Asciidoctor community of projects.
3
star
76

docker-asciidoctorj

Ensure that AsciidoctorJ can be used by apps into a Java Application Server (WildFly AS for now)
Java
3
star
77

asciidoctorj-chart

AsciidoctorJ Chart bundles the Asciidoctor Chart RubyGem (asciidoctor-chart) so it can be loaded into the JVM using JRuby.
HTML
3
star
78

asciidoctor-grunt-plugin

A Grunt plugin that uses Asciidoctor via Asciidoctor.js to process AsciiDoc source files within the project.
HTML
2
star
79

asciidoctor-rpm-mirror

A mirror of the package spec for the rubygem-asciidoctor (alias: asciidoctor) rpm. DO NOT PUSH CHANGES TO THIS MIRROR.
Ruby
2
star
80

default-to-asciidoc-chrome-extension

You love AsciiDoc and you want it to be the default option, this extension is for you!
JavaScript
2
star
81

asciidoctorj-epub3

AsciidoctorJ EPUB3 bundles the Asciidoctor EPUB3 RubyGem (asciidoctor-epub3) so it can be loaded into the JVM using JRuby.
Java
1
star
82

asciidoctor-docbook45

!DEPRECATED! An Asciidoctor converter that converts AsciiDoc to DocBook 4.5. This converter is community maintained and will not be released. Use the built-in DocBook 5 converter instead.
Ruby
1
star