Livewire Error The Command PDF To text Upload file / Library ini adalah wrapper PHP untuk binary pdftotext dari Poppler, berfungsi untuk mengubah file PDF menjadi teks secara akurat dan cepat.

 


📌 Upload PDF dan Konversi ke Teks di Laravel Livewire

1️⃣ Install library


composer require spatie/pdf-to-text

Install pdftotext di server:

    Ubuntu/Debian:


sudo apt-get install poppler-utils

CentOS:


sudo yum install poppler-utils

macOS:


    brew install poppler

2️⃣ Buat Livewire Component


php artisan make:livewire UploadPdfText

3️⃣ Edit UploadPdfText.php

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithFileUploads;
use Spatie\PdfToText\Pdf;

class UploadPdfText extends Component
{
    use WithFileUploads;

    public $file;
    public $parsedText;

    public function parsePdf()
    {
        $this->validate([
            'file' => 'required|file|mimes:pdf|max:20480', // Max 20MB
        ]);

        $path = $this->file->getRealPath();
        $text = Pdf::getText($path);

        $this->parsedText = $text;
    }

    public function render()
    {
        return view('livewire.upload-pdf-text');
    }
}

4️⃣ Buat upload-pdf-text.blade.php


<div class="p-6 bg-white border rounded shadow">
    <h2 class="text-lg font-semibold mb-4">Upload PDF dan Konversi ke Teks</h2>

    <input type="file" wire:model="file" accept="application/pdf" class="mb-2">
    <div wire:loading wire:target="file">Mengupload...</div>

    <button wire:click="parsePdf"
        class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">Proses PDF</button>

    @if ($parsedText)
        <div class="mt-4">
            <h3 class="font-semibold mb-2">Hasil Teks:</h3>
            <pre class="p-2 bg-gray-100 rounded max-h-[400px] overflow-auto text-sm">{{ $parsedText }}</pre>
        </div>
    @endif
</div>

5️⃣ Tambahkan ke Route atau Blade


Di halaman Blade:

@livewire('upload-pdf-text')

Atau route untuk testing:

Route::get('/upload-pdf', function () {
    return view('pdf-upload-page'); // berisi @livewire di atas
});

✅ Hasil


🔹 User upload file PDF.
🔹 Tekan tombol Proses PDF.
🔹 Teks dari PDF akan ditampilkan di layar, siap untuk di-copy atau disimpan ke DB.

JIKA TERJADI ERROR DAN BERMASALAH DENGAN /usr/bin/pdftotext

 Direkomendasikan: Gunakan Wrapper Script

Buat shell script kecil untuk membungkus pdftotext dengan LD_LIBRARY_PATH kosong, lalu arahkan Spatie ke script ini.
📁 Langkah-langkah:
1. Buat file /usr/local/bin/pdftotext-wrapper

Isi file dengan:

#!/bin/bash
LD_LIBRARY_PATH= /usr/bin/pdftotext "$@"

2. Jadikan script bisa dieksekusi:

chmod +x /usr/local/bin/pdftotext-wrapper

3. Ubah kode PHP:

use Spatie\PdfToText\Pdf;

$text = Pdf::getText('/path/to/file.pdf', '/usr/local/bin/pdftotext-wrapper');

🔚 Kesimpulan:

    LD_LIBRARY_PATH harus dikosongkan, jadi gunakan LD_LIBRARY_PATH=

    Tapi karena Pdf::getText() tidak mendukung parameter ketiga, kamu tidak bisa langsung menyetel env di situ

    Solusi paling bersih: Gunakan wrapper script yang menyetel env kosong, lalu arahkan Pdf::getText() ke script itu