PHP code example of andileco / php-epub

1. Go to this page and download the library: Download andileco/php-epub library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andileco / php-epub example snippets


use PhpEpub\Epub;
use PhpEpub\Metadata;
use PhpEpub\Chapter;
use PhpEpub\Resource;
use PhpEpub\Semantic;

$epub = Epub::create()
    ->metadata(
        Metadata::create('My Book')
            ->authors(['Jane Doe'])
            ->isbn('978-1234567890')
            ->publisher('Example Press')
    )
    ->stylesheet(Resource::fromFile('styles.css'))
    ->chapter(
        Chapter::create('Introduction', '<h1>Introduction</h1><p>Welcome.</p>')
            ->semantic(Semantic::INTRODUCTION)
    )
    ->chapter(
        Chapter::create('Chapter 1', '<h1>Getting Started</h1><p>Content here...</p>')
            ->semantic(Semantic::CHAPTER)
            ->subChapter(
                Chapter::create('Prerequisites', '<h2>Prerequisites</h2><p>You need...</p>')
            )
    )
    ->save('my-book.epub');

$epub = Epub::create()
    ->metadata($meta)
    ->stylesheet($css)
    ->chapter($ch1)
    ->chapter($ch2);

->chapter(Chapter::create('Preface', $html)->semantic(Semantic::PREFACE))
->chapter(Chapter::create('Chapter 1', $html)->semantic(Semantic::CHAPTER))
->chapter(Chapter::create('Glossary', $html)->semantic(Semantic::GLOSSARY))

->chapter(
    Chapter::create('Part One', '<p>Intro to part one.</p>')
        ->subChapter(Chapter::create('Section 1.1', '<p>Details.</p>'))
        ->subChapter(Chapter::create('Section 1.2', '<p>More details.</p>'))
)

$html = '<p>Here is a chart:</p>
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
  <circle cx="100" cy="100" r="80" fill="steelblue"/>
</svg>';

->chapter(Chapter::create('Data Visualization', $html))

$html = '<p>The quadratic formula:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>x</mi><mo>=</mo>
  <mfrac>
    <mrow><mo>-</mo><mi>b</mi><mo>±</mo><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>-</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow>
    <mrow><mn>2</mn><mi>a</mi></mrow>
  </mfrac>
</math>';

->chapter(Chapter::create('Equations', $html))

// Entire book as fixed layout
Epub::create()
    ->metadata(
        Metadata::create('My Cookbook')
            ->fixedLayout(1024, 768)  // width, height in CSS pixels
    )
    ->chapter(Chapter::create('Recipe 1', $htmlWithAbsolutePositioning))
    ->save('cookbook.epub');

use PhpEpub\Viewport;

Viewport::tablet()          // 1024 × 768
Viewport::tabletPortrait()  // 768 × 1024
Viewport::letter()          // 816 × 1056
Viewport::a4()              // 794 × 1123

use PhpEpub\Layout;
use PhpEpub\Spread;
use PhpEpub\Viewport;

Metadata::create('My Comic')
    ->layout(Layout::PRE_PAGINATED)
    ->viewport(Viewport::tablet())
    ->spread(Spread::LANDSCAPE)   // Two-page spread in landscape
    ->orientation('landscape')     // Prefer landscape reading

Epub::create()
    ->metadata(Metadata::create('Annual Report'))  // Reflowable by default
    ->chapter(Chapter::create('Introduction', '<p>Text content...</p>'))
    ->chapter(
        Chapter::create('Q4 Dashboard', $svgChartHtml)
            ->layout(Layout::PRE_PAGINATED)
            ->viewport(new Viewport(1024, 768))
    )
    ->chapter(Chapter::create('Conclusion', '<p>More text...</p>'))
    ->save('report.epub');

Metadata::create('My Book')
    ->accessibility(
        accessMode: ['textual', 'visual'],
        accessModeSufficient: ['textual'],
        features: ['structuralNavigation', 'tableOfContents', 'alternativeText'],
        summary: 'This book 

// From file
->resource(Resource::fromFile('/path/to/image.jpg', 'images/photo.jpg'))

// From string
->resource(Resource::fromString($svgData, 'images/chart.svg'))

->coverImage(Resource::fromFile('/path/to/cover.jpg'))

// Save to file
$epub->save('/path/to/book.epub');

// Get raw binary data
$data = $epub->build();

// Write to stream
$epub->writeTo($stream);
bash
composer