How I create PDF and EPUB for my eBook using PHP
One of the things that I do every year is to update my eBook called PHP 8 in a Nutshell. It’s a book that I’ve been writing for the past few years for every new release related to PHP 8 and its minor releases.
- Get
ibis-next
installed - Initialize the book
- The content
- Generating the PDF and EPUB files
- In closing
Now, many of you might be wondering how I create the PDF and EPUB versions of this book. TLDR; I use a tool called ibis-next to generate the PDF and EPUB versions of my book. It’s written in PHP and covers all the things that I need to have my eBook bubble up. It’s a breeze to work with since you can write your book entirely in Markdown.
Fun fact: This package, ibis-next, is the successor of ibis, a tool that I used to generate the PDF previously. But since ibis is no longer maintained, the guys at Hi Folks! decided to create ibis-next (based on ibis) but with some extra goodies.
Get ibis-next
installed
To get started, you first need to install ibis-next. You can do so using Composer like so.
composer global require hi-folks/ibis-next
I like to install it globally so that I can use it from anywhere on my system. But you can also install it locally in your project as well.
Initialize the book
Once you have ibis-next installed, you can initialize the book using the following command in an empty directory.
ibis-next init
This will generate the following files and directories:
- The
assets
directory, which contains theme files used for building the PDF and EPUB files. Since the process involves an intermediary step that temporarily converts the content to HTML, the theme files are in HTML and CSS to ensure the correct styling for the output files. - The
assets/fonts
directory, where you can download and use your TrueType Font (TTF) files. - The
assets/cover.jpg
, which is a sample book cover image. - The
assets/images
directory, containing sample images used in the sample book. - The
content
directory, which includes some sample Markdown files. You can edit these files or create new ones. - The
ibis.php
file, which contains the ibis-next configuration.
The content directory is the most important part of the book. All the Markdown files related to the book will live in this directory and ibis-next will use these files to generate the PDF and EPUB files.
I like to structure my content files in the following way.
As you can tell, I prepend the numbers to the files to ensure that they are processed in the correct order. And it makes it easier to maintain the book.
The content
A typical content file looks like so.
---
title: Introduction
---
# Introduction
This is the introduction to the book.
## Chapter 1
Awesome chapter of my book.
As you can tell, you can start the content of the file with a header (#
or ##
) and then the content of the file. Optionally, you can specify a title for the file using the title
frontmatter if you want to customize the text of the page header for each section.
Generating the PDF and EPUB files
Once you have the content files ready, you can generate the PDF files in light and dark mode using the following commands respectively.
# PDF in light mode
ibis-next pdf
# PDF in dark mode
ibis-next pdf dark
This will generate the PDF files in the /export
directory.
You can also create an EPUB file for your eBook using ibis-next which I use to self-publish my book on Amazon Kindle and Google Play Books.
ibis-next epub
Another fun fact: I was using Pandoc to generate EPUB for my book previously. The process was very tedious since I had to maintain two different sets of content files since Pandoc have a slightly different syntax for Markdown.
This will generate the EPUB files in the same directory as the PDF files.
In closing
And that’s pretty much it.
This is all you need to know to get started with ibis-next to create your eBooks. As I said, the package can be customized to make changes according to your needs in the ibis.php
file where you can change the fonts, style the cover position, configure your book’s sample, etc.
There’s a lot of advanced things you can do with ibis-next—aside blocks for instance—that I haven’t covered in this article.
So, if you’re interested, you can check out the ibis-next repository on GitHub to learn more.
Like this article?
Buy me a coffee👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.