Giter VIP home page Giter VIP logo

asciidoctor-pdf's Introduction

Asciidoctor PDF: A native PDF converter for AsciiDoc

Lo and behold, a native PDF converter for AsciiDoc built with Asciidoctor and Prawn!
No more middleman.
No more DocBook toolchain.
It’s AsciiDoc straight to PDF!

Status
Asciidoctor PDF is currently alpha software. While the converter handles most AsciiDoc content, there’s still work needed to fill in gaps where conversion is incomplete, incorrect or not implemented. See the milestone v1.5.0 in the issue tracker for details.

Prawn, the majestic PDF generator

Asciidoctor PDF is made possible by an amazing Ruby gem named Prawn. And what a gem it is!

Prawn is a nimble PDF writer for Ruby. More important, it’s a hackable platform that offers both high level APIs for the most common needs and low level APIs for bending the document model to accommodate special circumstances.

With Prawn, you can write text, draw lines and shapes and place images anywhere on the page and add as much color as you like. In addition, it brings a fluent API and aggressive code re-use to the printable document space.

Here’s an example that demonstrates how to use Prawn to create a basic PDF document.

Create a basic PDF document using Prawn
require 'prawn'

Prawn::Document.generate 'output.pdf' do
  text 'Hello, PDF creation!'
end

It’s that easy. And that’s just the beginning. Skip ahead to Getting started to start putting it use.

Prawn is the killer library for PDF generation we’ve needed to close this critical gap in Asciidoctor. It absolutely takes the pain out of creating printable documents. Picking up from there, Asciidoctor PDF takes the pain out of creating PDF documents from AsciiDoc.

Features

Notable features

  • Direct AsciiDoc to PDF conversion

  • Configuration-driven style and layout

  • PDF document outline (i.e., bookmarks)

  • Table of contents page(s)

  • Document metadata (title, authors, subject, keywords, etc)

  • Internal cross reference links

  • Syntax highlighting with CodeRay or Pygments

  • Page numbering

  • Customizable running content (header and footer)

  • “Keep together” blocks (i.e., page breaks avoided in certain block content)

  • Orphan section titles avoided

  • Table border settings honored

  • Font-based icons (currently admonition blocks only)

  • Custom fonts

Missing features

See WORKLOG.

Prerequisites

All that’s needed is Ruby (1.9.3 or above; 2.2.x recommended) and a few Ruby gems, which we explain how to install in the next section.

Warning

Prawn 2.0.0 and above requires Ruby >= 2.0.0 at installation (though it still works with Ruby 1.9.3 once you get beyond installation). If you need to use Asciidoctor PDF with Ruby 1.9.3, you must first install Prawn 1.3.0 using:

$ gem install prawn --version 1.3.0

You can then proceed with installation of Asciidoctor PDF.

To check you have Ruby available, use the ruby command to query the version installed:

$ ruby --version

Getting started

You can get Asciidoctor PDF by installing the published gem or running the code from source.

Install the published gem

Asciidoctor PDF is published in pre-release on RubyGems.org. You can install the published gem using the following command:

$ gem install --pre asciidoctor-pdf

If you want to syntax highlight source listings, you’ll also want to install CodeRay, Rouge or Pygments. Choose one (or more) of the following:

CodeRay
$ gem install coderay
Rouge
$ gem install rouge
Pygments
$ gem install pygments.rb

Assuming all the required gems install properly, verify you can run the asciidoctor-pdf script:

$ asciidoctor-pdf -v

If you see the version of Asciidoctor PDF printed, you’re ready to use Asciidoctor PDF.

Let’s grab an AsciiDoc document to distill and start putting Asciidoctor PDF to use!

An example AsciiDoc document

If you don’t already have an AsciiDoc document, you can use the basic-example.adoc file found in the examples directory of this project.

It’s time to convert the AsciiDoc document directly to PDF.

Convert AsciiDoc to PDF

Important
You’ll need to the coderay gem installed to run this example since it uses the source-highlighter attribute with the value of coderay.

Converting to PDF is a simple as running the asciidoctor-pdf script using Ruby and passing our AsciiDoc document as the first argument.

$ asciidoctor-pdf basic-example.adoc

This command is just a shorthand way of running:

$ asciidoctor -r asciidoctor-pdf -b pdf basic-example.adoc

When the script completes, you should see the file basic-example.pdf in the same directory. Open the basic-example.pdf file with a PDF viewer to see the result.

Screenshot of PDF document
Figure 1. Example PDF document rendered in a PDF viewer

You’re also encouraged to try converting this README as well as the documents in the examples directory to see more of what Asciidoctor PDF can do.

The pain of the DocBook toolchain should be melting away about now.

Themes

The layout and styling of the PDF is driven by a YAML configuration file. To learn how the theming system works and how to create and apply custom themes, refer to the Asciidoctor PDF Theme Guide. You can use the built-in theme files, which you can find in the data/themes directory, as examples.

Optional scripts

Asciidoctor PDF also provides a shell script that invokes GhostScript (gs) to optimize and compress the generated PDF with minimal impact on quality. You must have Ghostscript installed to use it.

Here’s an example usage:

$ ./bin/optimize-pdf basic-example.pdf

The command will generate the file example-optimized.pdf in the current directory.

Warning
The optimize-pdf script currently requires a Bash shell (Linux, OSX, etc). We plan to rewrite the script in Ruby so it works across platforms (see issue #1)
Important
The optimize-pdf script relies on Ghostscript >= 9.10. Otherwise, it may actually make the PDF larger. Also, you should only consider using it if the file size of the original PDF is > 5MB.

If a file is found with the extension .pdfmarks and the same rootname as the input file, it is used to add metadata to the generated PDF document. This file is necessary to preserve the document metadata since Ghostscript will otherwise drop it. That’s why Asciidoctor PDF always creates this file in addition to the PDF.

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.

Development

To help develop Asciidoctor PDF, or to simply use the development version, you need to get the source from GitHub. Follow the instructions below to learn how to clone the source and run it from your local copy.

Retrieve the source code

You can retrieve the source of Asciidoctor PDF 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 GitHub repository URL and pass it to git clone command:

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

Next, change to the project directory:

$ cd asciidoctor-pdf

Option 2: Download the archive

If you want to download a zip archive, click 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-pdf directory, you can simply add the absolute path of the bin directory to your PATH environment variable.

We’ll leverage the project configuration to install the necessary dependencies.

Install dependencies

If you’re using RVM, we recommend creating a new gemset to work with Asciidoctor PDF:

$ rvm use 2.2@asciidoctor-pdf --create

We like RVM because it keeps the dependencies required by various projects isolated.

The dependencies needed to use Asciidoctor PDF are defined in the Gemfile at the root of the project. We can use Bundler to install the dependencies for us.

To check you have Bundler available, use the bundle command to query the version installed:

$ bundle --version

If it’s not installed, use the gem command to install it.

$ gem install bundler

Then use the bundle command to install the project dependencies:

$ bundle
Note
You need to call bundle from the project directory so that it can find the Gemfile.

Assuming all the required gems install properly, verify you can run the asciidoctor-pdf script using Ruby:

$ ruby ./bin/asciidoctor-pdf -v

or

$ bundle exec ./bin/asciidoctor-pdf -v

If you see the version of Asciidoctor PDF printed, you’re ready to use Asciidoctor PDF!

Caution
If you get an error message—​and you’re not using a Ruby manager like RVM—​you may need to invoke the script through bundle exec: For best results, be sure to always use bundle exec whenever invoking the ./bin/asciidoctor-pdf script in development mode.

Authors

Asciidoctor PDF was written by Dan Allen and Sarah White of OpenDevise Inc. on behalf of the Asciidoctor Project.

Copyright © 2014 OpenDevise Inc. and the Asciidoctor Project. Free use of this software is granted under the terms of the MIT License.

For the full text of the license, see the LICENSE file. Refer to the NOTICE file for information about third-party Open Source software in use.

asciidoctor-pdf's People

Contributors

abatalev avatar chloerei avatar erik-brangs avatar fredkelly avatar graphitefriction avatar jessedoyle avatar leif81 avatar lightguard avatar mgreau avatar mojavelinux avatar olegsmetanin avatar orthographic-pedant avatar otavio avatar theimdal avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.