WebTools

307 Useful Tools & Utilities to make life easier.

FB2 to EPUB Converter

Convert FictionBook FB2 e-books into universally compatible EPUB archives.

Converts a .fb2 FictionBook into a standards-compliant EPUB3 archive (Max: 20MB)
Uploading…

Understanding the FB2 to EPUB3 Conversion Process

The FB2 to EPUB Converter is a server-side utility designed to transform FictionBook 2.0 documents (.fb2 or .xml) into standards-compliant EPUB3 archives. Because FB2 is essentially an XML-based format primarily popular in Eastern Europe, converting it to EPUB ensures maximum compatibility with modern international e-readers, tablets, and software like Apple Books, Amazon Kindle (via Send-to-Kindle), and Kobo.

This tool accepts files up to 20MB in size and handles the heavy lifting of XML parsing, structural segmentation, and ZIP archiving.

Deep Dive: How the Parsing Engine Works

Rather than relying on generic document conversion binaries, this tool uses a bespoke PHP-based processing pipeline tailored specifically for FictionBook schemas. The conversion sequence relies on PHP's SimpleXML and ZipArchive extensions, executing in several distinct phases:

  • Aggressive XML Sanitization: Older FB2 files frequently contain malformed namespaces, conflicting DOCTYPE declarations, or unusual XML headers. Before parsing, the tool uses regular expressions to strip out <?xml ... ?> declarations, <!DOCTYPE ... >, and inline xmlns attributes. This sanitization forces SimpleXML to read the document as a plain node tree, preventing fatal namespace exceptions.
  • Metadata Extraction: The tool targets the <description><title-info> block to retrieve the book-title, author first-name, author last-name, and lang. It combines the author's names, escapes HTML entities, and generates a random UUID for the EPUB's unique identifier.
  • Chapter Segmentation: The engine iterates through the FB2 <body> looking for <section> nodes. Each section is treated as an individual chapter and compiled into its own XHTML file. If an FB2 file lacks <section> tags, the engine gracefully falls back to wrapping the entire document into a single monolithic chapter. Empty chapters (those containing only whitespace or non-breaking spaces) are automatically skipped.
  • Tag Mapping and Transformation: The tool uses a specialized switch statement to map FB2 elements to standard HTML5. For example, <title> becomes <h2>, <p> remains <p>, and <empty-line> is translated to <br/>.
  • EPUB3 Package Assembly: An EPUB file is fundamentally a ZIP archive with a specific internal structure. The tool writes the mimetype file first with zero compression (ZipArchive::CM_STORE), which is a strict EPUB requirement. It then generates the package manifest (content.opf) and a navigation document (nav.xhtml) before bundling the individual chapter XHTML files.

Worked Example: Node Transformation

To illustrate how the internal parser reshapes your text, consider this simplified chunk of an FB2 input file:

<body>
  <section>
    <title><p>Chapter 1: The Arrival</p></title>
    <p>The ship docked at midnight.</p>
    <empty-line/>
    <p>No one was there to greet them.</p>
  </section>
</body>

When the tool processes this section, it extracts the title, sanitizes the text, applies its element mapping rules, and packages it into an internal EPUB component (e.g., chapter1.xhtml) that looks exactly like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="UTF-8"/>
  <title>Chapter 1: The Arrival</title>
  <style>
    body { font-family: serif; line-height: 1.7; margin: 1.5em; color: #111; }
    h2   { font-size: 1.4em; margin-top: 2em; }
    p    { margin: 0.6em 0; text-align: justify; }
  </style>
</head>
<body>
<h2>Chapter 1: The Arrival</h2>
<p>The ship docked at midnight.</p>
<br/>
<p>No one was there to greet them.</p>
</body>
</html>

Notice how the parser automatically injects a baseline CSS stylesheet ensuring comfortable readability (serif font, 1.7 line height, justified text) on standard devices.

Frequently Asked Questions

What is the maximum file size I can upload?

The tool strictly enforces a 20MB (20,480 KB) upload limit. If your FB2 file exceeds this size, the server validation will block the conversion. Large file sizes in FB2 are usually caused by heavily embedded base64 images.

Does this converter support embedded images or complex styling?

Currently, the parser is optimized for text extraction. It explicitly supports the structural tags <title>, <p>, <empty-line>, and nested <section> tags. Unrecognized tags, including inline formatting elements like bold/italic or embedded binaries (<binary>), are stripped out using PHP's strip_tags() function. The resulting text is preserved, but images and complex styles are discarded.

Why does the tool strip out XML namespaces?

FictionBook files compiled by various older software often contain mismatched or invalid XML namespace definitions. If passed directly to an XML parser, these cause fatal document-loading errors. By using regular expressions to strip out xmlns declarations before parsing, the tool guarantees that even poorly formatted FB2 files can be read successfully.

Is the output file EPUB2 or EPUB3?

The tool generates an EPUB 3.0 archive. It explicitly declares version="3.0" in the content.opf package document and uses the modern nav.xhtml structure for the table of contents, dropping the legacy NCX file format used in EPUB2.

Contact

Missing something?

Feel free to request missing tools or give some feedback using our contact form.

Contact Us