PHP code example of ubertech-za / asciidoctor-wrapper

1. Go to this page and download the library: Download ubertech-za/asciidoctor-wrapper 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/ */

    

ubertech-za / asciidoctor-wrapper example snippets


return [
    'executables' => [
        'asciidoctor' => env('ASCIIDOCTOR_PATH', 'asciidoctor'),
        'asciidoctor_pdf' => env('ASCIIDOCTOR_PDF_PATH', 'asciidoctor-pdf'),
    ],
    
    'default_theme' => env('ASCIIDOCTOR_DEFAULT_THEME', 'default'),
    'themes_path' => env('ASCIIDOCTOR_THEMES_PATH', storage_path('asciidoc/themes')),
    'templates_path' => env('ASCIIDOCTOR_TEMPLATES_PATH', storage_path('asciidoc/templates')),
    'fonts_path' => env('ASCIIDOCTOR_FONTS_PATH', storage_path('asciidoc/fonts')),
    'output_path' => env('ASCIIDOCTOR_OUTPUT_PATH', storage_path('asciidoc/documents')),
    
    // ... additional configuration
];

use UbertechZa\AsciidoctorWrapper\Facades\Asciidoctor;

// Convert AsciiDoc to HTML
$result = Asciidoctor::convert(
    input: 'document.adoc',
    output: 'document.html',
    format: 'html5'
);

// Convert to PDF
$result = Asciidoctor::convert(
    input: 'document.adoc',
    output: 'document.pdf',
    format: 'pdf'
);

if ($result['success']) {
    echo "Conversion successful!";
} else {
    echo "Error: " . $result['error'];
}

use UbertechZa\AsciidoctorWrapper\AsciidoctorWrapper;

$wrapper = new AsciidoctorWrapper(
    asciidoctorPath: '/usr/local/bin/asciidoctor',
    asciidoctorPdfPath: '/usr/local/bin/asciidoctor-pdf'
);

$result = $wrapper->convert('input.adoc', 'output.pdf', 'pdf');

use UbertechZa\AsciidoctorWrapper\Builder\ThemeBuilder;

$theme = app(ThemeBuilder::class)
    ->name('modern-theme')
    ->extending('default')
    ->withColors([
        'primary' => '#2563eb',
        'secondary' => '#64748b',
        'accent' => '#059669',
        'text' => '#0f172a',
        'background' => '#ffffff'
    ])
    ->withFont('heading', 'Inter', '16pt', 'bold')
    ->withFont('body', 'Inter', '11pt', 'normal')
    ->withFont('mono', 'JetBrains Mono', '9pt', 'normal')
    ->withElementStyle('document', [
        'color' => '#0f172a',
        'font' => ['family' => 'Inter', 'size' => '11pt']
    ])
    ->withElementStyle('heading1', [
        'color' => '#2563eb',
        'font' => ['family' => 'Inter', 'size' => '24pt', 'weight' => 'bold']
    ])
    ->build();

// Use the theme
$result = Asciidoctor::convert('input.adoc', 'output.pdf', 'pdf', $theme);

use UbertechZa\AsciidoctorWrapper\StyleManager;

$styleManager = app(StyleManager::class);
$theme = $styleManager->loadFromJson(resource_path('asciidoctor/themes/my-theme.json'));

$result = Asciidoctor::convert('input.adoc', 'output.pdf', 'pdf', $theme);

'fonts_path' => env('ASCIIDOCTOR_FONTS_PATH', storage_path('asciidoc/fonts')),

$theme = app(ThemeBuilder::class)
    ->name('custom-font-theme')
    ->extending('default-with-font-fallbacks')
    ->withColors([
        'primary' => '#2563eb',
        'text' => '#0f172a'
    ])
    ->build();

// Generate multiple formats
$formats = ['html5', 'pdf', 'docx'];

foreach ($formats as $format) {
    $result = Asciidoctor::convert(
        input: 'document.adoc',
        output: "document.{$format}",
        format: $format,
        theme: $theme
    );
}

$result = Asciidoctor::convert(
    input: 'document.adoc',
    output: 'document.html',
    format: 'html5',
    attributes: [
        'source-highlighter' => 'rouge',
        'icons' => 'font',
        'sectanchors' => true,
        'toc' => 'left',
        'toclevels' => 3
    ]
);

$result = Asciidoctor::convert(
    input: 'document.adoc',
    output: 'document.html',
    format: 'html5',
    safeMode: 'safe' // unsafe, safe, server, secure
);

use UbertechZa\AsciidoctorWrapper\Style\ColorStyle;

$colors = new ColorStyle(
    primary: '#2563eb',
    secondary: '#64748b',
    accent: '#059669',
    text: '#0f172a',
    background: '#ffffff'
);

// Validate colors
if ($colors->isValidHex('#2563eb')) {
    echo "Valid hex color!";
}

use UbertechZa\AsciidoctorWrapper\Style\FontStyle;

$font = new FontStyle(
    family: 'Inter',
    size: '12pt',
    weight: 'bold',
    style: 'normal'
);

// Convert to array for processing
$fontArray = $font->toArray();

use UbertechZa\AsciidoctorWrapper\Style\ElementStyle;

$heading = new ElementStyle(
    color: '#2563eb',
    backgroundColor: '#ffffff',
    font: new FontStyle('Inter', '18pt', 'bold'),
    margin: '1em 0',
    padding: '0.5em'
);

$wrapper = app(AsciidoctorWrapper::class);

if ($wrapper->isAsciidoctorAvailable()) {
    echo "AsciiDoctor is available!";
}

if ($wrapper->isAsciidoctorPdfAvailable()) {
    echo "AsciiDoctor-PDF is available!";
}

$formats = $wrapper->getSupportedFormats();
// ['html', 'html5', 'docbook', 'docbook5', 'manpage', 'pdf', 'docx']

use UbertechZa\AsciidoctorWrapper\StyleManager;

$styleManager = app(StyleManager::class);
$theme = $styleManager->loadFromJson('theme.json');

if ($styleManager->validateTheme($theme)) {
    echo "Theme is valid!";
} else {
    $errors = $styleManager->getValidationErrors();
    foreach ($errors as $error) {
        echo "Error: {$error}\n";
    }
}



namespace App\Console\Commands;

use Illuminate\Console\Command;
use UbertechZa\AsciidoctorWrapper\Facades\Asciidoctor;
use UbertechZa\AsciidoctorWrapper\Builder\ThemeBuilder;

class ConvertToAsciidoc extends Command
{
    protected $signature = 'asciidoc:convert {input} {output} {--format=html5} {--theme=}';
    protected $description = 'Convert AsciiDoc files to various formats';

    public function handle()
    {
        $input = $this->argument('input');
        $output = $this->argument('output');
        $format = $this->option('format');
        $themeName = $this->option('theme');
        
        $theme = null;
        if ($themeName) {
            $theme = app(ThemeBuilder::class)->name($themeName)->build();
        }
        
        $result = Asciidoctor::convert($input, $output, $format, $theme);
        
        if ($result['success']) {
            $this->info("✅ Successfully converted {$input} to {$output}");
        } else {
            $this->error("❌ Conversion failed: " . $result['error']);
        }
    }
}

$result = Asciidoctor::convert('input.adoc', 'output.pdf', 'pdf');

if ($result['success']) {
    echo "Success! Output: " . $result['output'];
} else {
    echo "Error: " . $result['error'];
    echo "Exit Code: " . $result['exit_code'];
    if (!empty($result['stderr'])) {
        echo "STDERR: " . $result['stderr'];
    }
}

'cache' => [
    'enabled' => env('ASCIIDOCTOR_CACHE_ENABLED', true),
    'path' => storage_path('app/asciidoctor/cache'),
    'ttl' => env('ASCIIDOCTOR_CACHE_TTL', 3600),
],
bash
php artisan vendor:publish --tag=asciidoctor-config