WebTools

307 Useful Tools & Utilities to make life easier.

FB2 to PDF Converter

Render FictionBook FB2 formatting securely natively out into PDF layouts.

Unpack and compile robust .fb2 archives natively into scalable PDF documents (Max: 20MB)

Behind the Scenes: How the FB2 to PDF Converter Operates

The FB2 to PDF Converter provides a robust, native rendering pipeline to transform FictionBook 2.0 (.fb2) archives and generic XML ebook payloads into standard, portable PDF documents. Rather than relying on external web services, this tool utilizes a server-side conversion architecture powered by Laravel Livewire and the renowned Dompdf PHP library to handle the transformation process from start to finish.

1. Payload Validation and Ingestion

The ingestion process begins by verifying the structural limits and file integrity of the uploaded document. The application enforces a hard 20MB maximum memory limit to prevent server exhaustion during large DOM rendering sequences. Valid files must end with either a .fb2 or .xml extension. If a file passes this checkpoint, it is read directly into memory via native PHP file operations.

2. Structural Extraction via Regular Expressions

FictionBook files are essentially massive XML structures. Instead of relying on rigid, memory-heavy XML parsers (which often fail if an ebook has slightly malformed schemas), this converter employs a more aggressive, resilient string-matching approach:

  • Body Extraction: Using regular expressions (/<body[^>]*>(.*?)</body>/is), the tool specifically targets the <body> tree of the document. Any XML metadata located in the <description> block (such as cover images, author bios, or publishing info) is intentionally discarded to focus purely on the readable text.
  • Title Mapping: FictionBook <title> blocks are mapped and replaced with standard HTML5 <h2> headers to ensure proper visual hierarchy in the final PDF.
  • Tag Stripping: A strict strip_tags sequence is executed on the extracted body. Only essential structural and typographical tags are allowed to pass through: <h1> through <h6>, <p>, <br>, <b>, <i>, <strong>, <em>, <ul>, <ol>, <li>, <blockquote>, <span>, and <div>. Everything else—including images (<image>), scripts, or custom XML namespaces—is safely neutralized.

3. Dynamic DOM Generation and CSS Injection

Once the text is cleaned, it is wrapped in a dynamic, responsive HTML5 boilerplate designed specifically for PDF rendering. The engine injects standardized styling rules to ensure the resulting book is readable:

  • Global Font: The base font family is locked to Helvetica/Arial in a dark grey (#111) for sharp contrast and crisp printing.
  • Paragraph Styling: Text paragraphs (<p>) are automatically justified (text-align: justify) with a 1.6 line height and a legible 13.5pt font size.
  • Header Styling: Extracted titles (now <h2> elements) are rendered bold at 18pt in pure black, forced to the center of the page with ample padding.

4. PDF Rendering with Dompdf

The dynamically constructed HTML document is passed into the Dompdf engine. The engine is configured with the HTML5 parser enabled and uses A4 Portrait paper dimensions as the default canvas. Dompdf builds the visual flow, calculating page breaks and line wrapping based on the injected CSS constraints.

Once rendering is complete, a binary PDF is generated and temporarily written to the server's storage using a randomized hash filename. Upon triggering the download, Laravel seamlessly streams the file to your browser and subsequently deletes it from the server structure (deleteFileAfterSend(true)) to ensure data privacy and prevent disk bloat.

Worked Example: XML Transformation

Let’s observe how the tool transforms a minimal snippet from an FB2 file.

Input (.fb2 source):

<FictionBook>
  <description>
    <title-info><book-title>The Sample</book-title></title-info>
  </description>
  <body>
    <title><p>Chapter 1</p></title>
    <p>This is the <emphasis>first</emphasis> paragraph.</p>
    <image l:href="#cover.jpg"/>
  </body>
</FictionBook>

Processing Sequence:

  1. The preg_match ignores the <description> node entirely and isolates the contents of <body>.
  2. The <title> tags are converted to <h2>, resulting in <h2><p>Chapter 1</p></h2>.
  3. The strip_tags function sweeps the text. The <emphasis> tag is not on the allowed list, so it is stripped (leaving the inner text "first"). The <image> tag is also destroyed.
  4. Final HTML sent to Dompdf:
    <!DOCTYPE html>
    <html>
    <head>
      <style>...injected css...</style>
    </head>
    <body>
      <h2><p>Chapter 1</p></h2>
      <p>This is the first paragraph.</p>
    </body>
    </html>

The resulting PDF displays a centered, bold "Chapter 1" followed by a justified paragraph. The image is absent.

Frequently Asked Questions

Why did my book's cover image disappear?

The converter utilizes a strict text-extraction approach. Because standard web image tags (<img>) and FictionBook image tags (<image>, along with embedded binary image data) are purposely excluded from the allowed structural tags during the cleaning phase, all visual media is removed. The tool exclusively generates text-based PDFs.

What happens if my file doesn't have a <body> tag?

If the file's XML schema is malformed or lacks standard FictionBook <body>...</body> nodes, the regular expression targeting sequence will fail. The tool will halt the conversion and return a specific "Structural check failed" error indicating it could not locate the mapping tree.

Does the tool preserve the book's metadata like author and publisher?

No. By design, the tool specifically targets the <body> sequence of the file. FictionBook stores author, publisher, and genre metadata inside a separate <description> block which is located outside the body element. Therefore, metadata will not appear in the generated PDF.

Can I use a custom font for my PDF?

The internal renderer (Dompdf) is hardcoded to enforce Helvetica and Arial sans-serif fonts in the stylesheet. Custom embedded fonts inside the .fb2 file are stripped during the strip_tags phase, ensuring the resulting PDF is universally legible and processes quickly without missing glyph errors.

Contact

Missing something?

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

Contact Us