PHP code example of waterloobae / crowdmarkapilaravel

1. Go to this page and download the library: Download waterloobae/crowdmarkapilaravel 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/ */

    

waterloobae / crowdmarkapilaravel example snippets


// bootstrap/providers.php

return [
    // ...
    Waterloobae\CrowdmarkApiLaravel\Providers\CrowdmarkApiLaravelServiceProvider::class,
];

// config/services.php

return [
    // ... existing services

    'crowdmark' => [
        'base_url' => env('CROWDMARK_BASE_URL', 'https://app.crowdmark.com/'),
        'api_key' => env('CROWDMARK_API_KEY'),
    ],
];

use Illuminate\Support\Facades\Route;

Route::middleware('web')->group(
    base_path('vendor/waterloobae/crowdmarkapilaravel/routes/web.php')
);

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Waterloobae\CrowdmarkApiLaravel\Jobs\GenerateCrowdmarkBookletPagesJsonJob;
use Waterloobae\CrowdmarkApiLaravel\Jobs\GenerateCrowdmarkPagesPdfJob;
use Waterloobae\CrowdmarkApiLaravel\Jobs\GenerateCrowdmarkOddPagesPdfJob;

Route::get('/crowdmark', function () {
    return view('crowdmark');
})->name('crowdmark');

Route::post('/crowdmark/save-booklet-pages-json', function (Request $request) {
    $assessmentIds = array_values(array_filter(
        array_map('trim', explode(',', $request->input('assessment_ids', '')))
    ));

    if (empty($assessmentIds)) {
        return response()->json(['error' => 'No assessment IDs provided.'], 422);
    }

    $forceRefresh = filter_var($request->input('force_refresh', false), FILTER_VALIDATE_BOOL);
    $jsonPath = trim((string) $request->input('json_path', '')) ?: null;
    $jsonDisk = trim((string) $request->input('json_disk', '')) ?: null;

    $token = \Illuminate\Support\Str::uuid()->toString();
    Cache::put("crowdmark_json_{$token}", ['status' => 'pending'], now()->addHours(24));

    GenerateCrowdmarkBookletPagesJsonJob::dispatch($token, $assessmentIds, $forceRefresh, $jsonPath, $jsonDisk);

    return response()->json(['token' => $token]);
})->name('crowdmark.save-booklet-pages-json');

Route::get('/crowdmark/json-status/{token}', function (string $token) {
    $raw = Cache::get("crowdmark_json_{$token}");

    if ($raw === null) {
        return response()->json(['status' => 'not_found'], 404);
    }

    if (($raw['status'] ?? '') === 'done') {
        $cacheKey = (string) ($raw['cache_key'] ?? '');
        $savedPath = (string) ($raw['path'] ?? '');

        return response()->json([
            'status' => 'done',
            'cache_key' => $cacheKey,
            'disk' => (string) ($raw['disk'] ?? 'local'),
            'count' => (int) ($raw['count'] ?? 0),
            'created_at' => (string) ($raw['created_at'] ?? ''),
            'download_url' => $savedPath !== ''
                ? route('crowdmark.download-booklet-pages-json-by-token', ['token' => $token])
                : route('crowdmark.download-booklet-pages-json', ['cacheKey' => $cacheKey]),
        ]);
    }

    if (($raw['status'] ?? '') === 'failed') {
        return response()->json([
            'status' => 'failed',
            'error' => (string) ($raw['error'] ?? 'Unknown job failure.'),
        ]);
    }

    return response()->json(['status' => 'pending']);
})->name('crowdmark.json-status');

Route::get('/crowdmark/booklet-pages-json/{cacheKey}', function (string $cacheKey) {
    $path = storage_path('app/crowdmark-cache/' . $cacheKey . '.json');
    if (!is_file($path)) {
        abort(404, 'JSON cache file not found.');
    }

    return response()->download($path, 'crowdmark_booklet_pages_' . $cacheKey . '.json', [
        'Content-Type' => 'application/json',
    ]);
})->name('crowdmark.download-booklet-pages-json');

Route::get('/crowdmark/booklet-pages-json-token/{token}', function (string $token) {
    $raw = Cache::get("crowdmark_json_{$token}");
    $path = (string) ($raw['path'] ?? '');
    $disk = trim((string) ($raw['disk'] ?? 'local'));

    if (!is_array($raw) || ($raw['status'] ?? '') !== 'done' || $path === '') {
        abort(404, 'JSON cache file not ready.');
    }

    // Backward compatibility for old absolute-path payloads.
    if (is_file($path)) {
        return response()->download($path, basename($path) ?: 'crowdmark_booklet_pages.json', [
            'Content-Type' => 'application/json',
        ]);
    }

    if (!Storage::disk($disk)->exists($path)) {
        abort(404, 'JSON cache file not found.');
    }

    return response()->streamDownload(function () use ($disk, $path): void {
        $stream = Storage::disk($disk)->readStream($path);
        if ($stream === false) {
            throw new \RuntimeException('Failed to open JSON stream.');
        }

        fpassthru($stream);
        fclose($stream);
    }, basename($path) ?: 'crowdmark_booklet_pages.json', [
        'Content-Type' => 'application/json',
    ]);
})->name('crowdmark.download-booklet-pages-json-by-token');

Route::post('/crowdmark/download-pages', function (Request $request) {
    $assessmentIds = array_values(array_filter(
        array_map('trim', explode(',', $request->input('assessment_ids', '')))
    ));
    $pageUuidInput = $request->input('page_uuid', []);
    $pageUuids = is_array($pageUuidInput)
        ? array_values(array_filter(array_map('trim', $pageUuidInput)))
        : array_values(array_filter(preg_split('/[,\r\n]+/', trim((string) $pageUuidInput)) ?: []));
    $jsonPath = trim((string) $request->input('json_path', '')) ?: null;
    $jsonDisk = trim((string) $request->input('json_disk', '')) ?: null;
    $pdfSavePath = trim((string) $request->input('pdf_save_path', '')) ?: null;
    $pdfDisk = trim((string) $request->input('pdf_disk', '')) ?: null;

    if (empty($assessmentIds)) {
        return response()->json(['error' => 'No assessment IDs provided.'], 422);
    }
    if (empty($pageUuids)) {
        return response()->json(['error' => 'No page UUID provided.'], 422);
    }

    $token = \Illuminate\Support\Str::uuid()->toString();
    Cache::put("crowdmark_pdf_{$token}", 'pending', now()->addHours(2));

    GenerateCrowdmarkPagesPdfJob::dispatch(
        $token,
        $assessmentIds,
        $pageUuids,
        $jsonPath,
        $jsonDisk,
        $pdfSavePath,
        $pdfDisk,
    );

    return response()->json(['token' => $token]);
})->name('crowdmark.download-pages');

Route::get('/crowdmark/pdf-status/{token}', function (string $token) {
    $raw = Cache::get("crowdmark_pdf_{$token}");

    if ($raw === null) {
        return response()->json(['status' => 'not_found'], 404);
    }

    if (str_starts_with((string) $raw, 'failed:')) {
        return response()->json(['status' => 'failed', 'error' => substr((string) $raw, 7)]);
    }

    return response()->json(['status' => $raw]);
})->name('crowdmark.pdf-status');

Route::get('/crowdmark/pdf-download/{token}', function (string $token) {
    $status = Cache::get("crowdmark_pdf_{$token}");
    if ($status !== 'done') {
        abort(404, 'PDF not ready.');
    }

    $path = (string) Cache::get("crowdmark_pdf_path_{$token}", "crowdmark-pdfs/{$token}.pdf");
    $disk = (string) Cache::get("crowdmark_pdf_disk_{$token}", 'local');
    if (!Storage::disk($disk)->exists($path)) {
        abort(404, 'PDF file missing.');
    }

    return response()->streamDownload(function () use ($disk, $path): void {
        $stream = Storage::disk($disk)->readStream($path);
        if ($stream === false) {
            throw new \RuntimeException('Failed to open PDF stream.');
        }

        fpassthru($stream);
        fclose($stream);
    }, basename($path) ?: "pages_{$token}.pdf", ['Content-Type' => 'application/pdf']);
})->name('crowdmark.pdf-download');

Route::post('/crowdmark/download-odd-pages', function (Request $request) {
    $assessmentIds = array_values(array_filter(
        array_map('trim', explode(',', $request->input('assessment_ids', '')))
    ));

    $jsonPath = trim((string) $request->input('json_path', '')) ?: null;
    $jsonDisk = trim((string) $request->input('json_disk', '')) ?: null;
    $zipSavePath = trim((string) $request->input('zip_save_path', '')) ?: null;
    $zipDisk = trim((string) $request->input('zip_disk', '')) ?: null;

    if (empty($assessmentIds)) {
        return response()->json(['error' => 'No assessment IDs provided.'], 422);
    }

    $token = \Illuminate\Support\Str::uuid()->toString();
    Cache::put("crowdmark_pdf_{$token}", 'pending', now()->addHours(24));

    GenerateCrowdmarkOddPagesPdfJob::dispatch(
        $token,
        $assessmentIds,
        (int) $request->input('max_page', 39),
        $jsonPath,
        $jsonDisk,
        $zipSavePath,
        $zipDisk,
    );

    return response()->json(['token' => $token]);
})->name('crowdmark.download-odd-pages');

Route::get('/crowdmark/zip-download/{token}', function (string $token) {
    $status = Cache::get("crowdmark_pdf_{$token}");
    if ($status !== 'done') {
        abort(404, 'ZIP not ready.');
    }

    $path = (string) Cache::get("crowdmark_pdf_path_{$token}", "crowdmark-pdfs/{$token}.zip");
    $disk = (string) Cache::get("crowdmark_pdf_disk_{$token}", 'local');
    if (!Storage::disk($disk)->exists($path)) {
        abort(404, 'ZIP file missing.');
    }

    return response()->streamDownload(function () use ($disk, $path): void {
        $stream = Storage::disk($disk)->readStream($path);
        if ($stream === false) {
            throw new \RuntimeException('Failed to open ZIP stream.');
        }

        fpassthru($stream);
        fclose($stream);
    }, basename($path) ?: "odd_pages_{$token}.zip", ['Content-Type' => 'application/zip']);
})->name('crowdmark.zip-download');

use Filament\Pages\Dashboard;
use Waterloobae\CrowdmarkApiLaravel\Filament\Pages\CrowdmarkExample;

// in panel() chain
->pages([
    Dashboard::class,
    CrowdmarkExample::class,
])
bash
php artisan queue:work --timeout=0 --tries=1