1. Go to this page and download the library: Download adrorocker/epub-builder 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/ */
use Symfony\Component\HttpFoundation\StreamedResponse;
return new StreamedResponse(
function () use ($builder) {
$builder->buildToStream(fopen('php://output', 'wb'));
},
200,
[
'Content-Type' => 'application/epub+zip',
'Content-Disposition' => 'attachment; filename="book.epub"',
],
);
use AdroSoftware\EpubBuilder\Contracts\NodeHandlerInterface;
use AdroSoftware\EpubBuilder\Renderers\DefaultTiptapRenderer;
final class CalloutHandler implements NodeHandlerInterface
{
public function render(array $node, callable $renderChildren): string
{
$type = htmlspecialchars($node['attrs']['type'] ?? 'info', ENT_XML1 | ENT_QUOTES, 'UTF-8');
$content = $renderChildren($node['content'] ?? []);
return "<aside class=\"callout callout--{$type}\">{$content}</aside>\n";
}
}
$builder = EpubBuilder::create();
$renderer = $builder->getRenderer();
if ($renderer instanceof DefaultTiptapRenderer) {
$renderer->register('callout', new CalloutHandler());
}
use AdroSoftware\EpubBuilder\Contracts\NodeHandlerInterface;
use AdroSoftware\EpubBuilder\Epub\AssetManager;
final class UnsplashImageHandler implements NodeHandlerInterface
{
public function __construct(private readonly AssetManager $assetManager) {}
public function render(array $node, callable $renderChildren): string
{
$src = (string) ($node['attrs']['src'] ?? '');
if ($src === '') return '';
// AssetManager downloads remote URLs into OEBPS/images/ at zip-write time.
// The 'jpg' hint is for URLs without a file extension in the path.
$epubSrc = $this->assetManager->addImage($src, 'jpg');
$alt = htmlspecialchars($node['attrs']['alt'] ?? '', ENT_XML1 | ENT_QUOTES, 'UTF-8');
return "<figure><img src=\"{$epubSrc}\" alt=\"{$alt}\"/></figure>\n";
}
}
$builder->getRenderer()->register('unsplashImage', new UnsplashImageHandler(
$builder->assetManager(),
));
$renderer->override('image', new MyCdnImageHandler($builder->assetManager()));
use AdroSoftware\EpubBuilder\Contracts\MarkHandlerInterface;
final class SmallCapsMark implements MarkHandlerInterface
{
public function render(string $content, array $mark): string
{
return "<span class=\"smallcaps\">{$content}</span>";
}
}
$renderer->registerMark('smallCaps', new SmallCapsMark());
use AdroSoftware\EpubBuilder\Contracts\ContentRendererInterface;
final class MyRenderer implements ContentRendererInterface
{
public function render(array $tiptapJson): string
{
// Your own rendering logic. Return a well-formed XHTML body fragment
// (no <html>, <body> wrapper — the library wraps it).
return $myExistingTiptapEngine->toHtml($tiptapJson);
}
}
EpubBuilder::create()
->setRenderer(new MyRenderer())
->setMetadata(fn($m) => $m->title('Book')->author('A'))
->addChapter('One', $json)
->build();
// Replace the default with your own CSS file
$builder->setStylesheet('/path/to/my-styles.css');
// Or append additional rules on top of the default
$builder->addCss('
body { font-family: "Iowan Old Style", Georgia, serif; }
blockquote { font-style: italic; }
');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.