PHP code example of 1tomany / pdf-pack-bundle

1. Go to this page and download the library: Download 1tomany/pdf-pack-bundle 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/ */

    

1tomany / pdf-pack-bundle example snippets




namespace App\File\Action\Handler;

use OneToMany\PdfPack\Contract\Action\ExtractActionInterface;
use OneToMany\PdfPack\Contract\Action\ReadActionInterface;
use OneToMany\PdfPack\Request\ExtractRequest;
use OneToMany\PdfPack\Request\ReadRequest;

final readonly class UploadFileHandler
{
    public function __construct(
        private ReadActionInterface $readAction,
        private ExtractActionInterface $extractAction,
    ) {
    }

    public function handle(string $filePath): void
    {
        // Read PDF metadata like page count
        $request = new ReadRequest($filePath);

        // @see OneToMany\PdfPack\Response\ReadResponse
        $response = $this->readAction->act($request);

        // Rasterize all pages of a PDF
        $request = new ExtractRequest($filePath)
            ->fromPage(1) // First page to extract
            ->toPage(null) // Last page to extract, NULL for all pages
            ->asPngOutput() // Generate PNG images
            ->atResolution(150); // At 150 DPI

        // @see OneToMany\PdfPack\Response\ExtractResponse
        foreach ($this->extractAction->act($request) as $page) {
            // $page->getData() or $page->toDataUri()
        }

        // Extract text from pages 2 through 8
        $request = new ExtractRequest($filePath, 2, 8)->asTextOutput();

        // @see OneToMany\PdfPack\Response\ExtractResponse
        foreach ($this->extractAction->act($request) as $page) {
            // $page->getData() or $page->toDataUri()
        }
    }
}



namespace App\PdfPack\Client\Magick;

use OneToMany\PdfPack\Contract\Client\ClientInterface;
use OneToMany\PdfPack\Contract\Request\ExtractRequest;
use OneToMany\PdfPack\Contract\Request\ReadRequest;
use OneToMany\PdfPack\Contract\Response\ReadResponse;

final readonly class MagickClient implements ClientInterface
{
    public function read(ReadRequest $request): ReadResponse
    {
        // Add your implementation here
    }

    public function extract(ExtractRequest $request): \Generator
    {
        // Add your implementation here
    }
}