WebTools

307 Useful Tools & Utilities to make life easier.

EPUB to FB2 Converter

Convert EPUB e-book archives into FictionBook FB2 XML format natively.

Converts an .epub e-book into a standards-compliant FictionBook .fb2 XML file (Max: 20MB)
Uploading…

Technical Overview of the EPUB to FB2 Converter

The EPUB to FB2 Converter is a server-side utility that transforms electronic publication files (.epub) into the FictionBook 2.0 (.fb2) XML format. Instead of depending on external command-line binaries like Calibre or Pandoc, this conversion engine relies entirely on PHP's native ZipArchive library. It opens the EPUB as a ZIP package, systematically deconstructs its internal file structure, and synthesizes a compliant XML file from scratch.

Internal Conversion Mechanics

EPUB files follow a strict directory structure and manifest format. The conversion process strictly follows these technical steps to ensure accurate translation of content:

  • OPF Discovery: The converter immediately targets the META-INF/container.xml file upon opening the archive. It parses this XML to find the full-path attribute, which points to the location of the Open Packaging Format (OPF) file. Without this file, the parser throws an exception, aborting the process.
  • Metadata Extraction: The system reads the OPF file and uses regular expressions to isolate the Dublin Core (<dc:title>, <dc:creator>, and <dc:language>) tags. The creator string is split at the first space to isolate the first and last names of the author.
  • Spine Processing: To maintain the correct chapter sequence, the parser maps all assets via the <item> tags and then builds an ordered array by reading the <itemref> IDs within the spine. This guarantees the FB2 document flows exactly as the publisher intended.
  • Text Parsing Engine: The engine loops through each chapter file designated in the spine. It first attempts to grab a chapter title from <title> or <h1> to <h6> tags. To extract the body, it aggressively targets standard <p> tags. It uses a filtering mechanism that strips empty HTML entities and non-breaking spaces (like &nbsp; or \xc2\xa0). If the document lacks paragraph tags, a fallback script executes that strips all HTML and grabs plain text line-by-line, rejecting empty lines to prevent bloated FB2 generation.
  • FB2 XML Generation: The final stage constructs the <FictionBook> XML string. It automatically assigns the document genre to literature, embeds the extracted metadata into the <title-info> block, generates a UUID for the <document-info>, and maps each extracted chapter into nested <section> and <p> nodes.

Tool Capabilities and File Limitations

The converter enforces a specific set of rules to ensure performance and avoid memory exhaustion during the recursive HTML parsing:

  • Allowed Extensions: The upload requires a strict .epub extension match.
  • Payload Size: The application is configured with a max:20480 validator, strictly limiting incoming EPUB archives to 20 Megabytes.
  • Temporary Storage: The compiled FB2 document is temporarily stored using a randomized identifier (e.g., epub_fb2_[random].fb2) and is provided for download before being automatically pruned by the server.

Concrete Translation Example

Consider an EPUB with a metadata tag defining the author as <dc:creator>Jane Doe</dc:creator>. Because FB2 expects distinct name tags, the PHP backend separates the first string segment up to the first space. Here is exactly how the EPUB metadata translates to FB2:

// Parsed From EPUB OPF:
<dc:title>A Scientific Journey</dc:title>
<dc:creator>Jane Doe</dc:creator>
<dc:language>en</dc:language>

// Synthesized FB2 Structure:
<title-info>
  <genre>literature</genre>
  <author>
    <first-name>Jane</first-name>
    <last-name>Doe</last-name>
  </author>
  <book-title>A Scientific Journey</book-title>
  <lang>en</lang>
</title-info>

Frequently Asked Questions

What happens if my EPUB file is missing author or title metadata?

If the parser cannot locate the <dc:title> tag in the OPF, it automatically defaults to "Untitled". If the <dc:language> is missing, it falls back to "en" (English). A missing author tag simply results in empty <first-name> and <last-name> elements inside the FB2 XML.

Why might an EPUB file result in the error "Cannot open EPUB — file may be corrupt"?

Because the conversion script utilizes PHP's ZipArchive, the uploaded file must be a perfectly compliant, uncorrupted ZIP package. If the file extension was merely renamed to .epub, or if the internal headers of the ZIP format are broken, the archive cannot be opened and this specific exception is thrown.

Does the tool preserve images or CSS styling?

No, the extraction logic specifically targets raw text. By filtering chapter content through regular expressions searching for <p> tags and using strip_tags(), all inline styles, CSS files, custom fonts, and images are intentionally discarded to output a clean, text-only FictionBook 2.0 file.

What if a chapter contains no readable text?

The parser ignores elements comprised solely of whitespace or non-breaking spaces. If a chapter file in the EPUB's spine is processed but yields zero readable paragraphs (such as a page containing only an image), the converter skips generating a <section> for that specific file.

Contact

Missing something?

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

Contact Us