oliverbj
oliverbj

Reputation: 6052

PHP / JSON - Return large string result in error

I am trying to convert a PDF file to raw text. This works fine actually, however - I am not able to return the converted text with JSON.

This is how I convert the PDF to Text:

$text = (new Pdf('/usr/local/bin/pdftotext'))
->setPdf(storage_path() . '/app/temp_files/' . $name)
->text();

$text = iconv('latin5', 'utf-8', $text); //Convert foreign characters

Now if I return the $text in normal JSON like this:

return response()->json([
            'result' => $text
], 200);

I get a Server Error message.

However, if I limit the string to for example 100 characters, it works fine:

return response()->json([
       'result' => str_limit($text, 100)
], 200);

Returns:

{
    "result": "Fuldstændig express DET EUROPÆISKE FÆLLESKAB 1ANGIVELSE 8 2 Afsender / Eksportør nr. IM MAEDEN INTER..."
}

How can I get it to return the entire text? The original PDF is a 2 pages long.

Upvotes: 0

Views: 110

Answers (1)

Joseph_J
Joseph_J

Reputation: 3669

This sounds like JSON is running into an encoding error.

Replace:

 $text = iconv('latin5', 'utf-8', $text); //Convert foreign characters

With:

$text = mb_convert_encoding($text, 'UTF-8', 'UTF-8');  //Force the UTF-8 encoding.

Hope it helps.

Upvotes: 1

Related Questions