Embed PDF with base64 and PHP greater than 6MB in an html

1

I have the following problem, I need to generate an html with a pdf embedded inside an embed, the interesting thing is that with files smaller than 6MB there is no problem but when the file is bigger it fails and only a white screen appears.

How do I make the bigger file appear like the smaller one?

I accept all kinds of suggestions.

Thank you very much in advance.

    $src = urldecode($_REQUEST['archivo']);
    //$src <-- string 'Z:/327000360.pdf'

    ini_set('memory_limit', '256M');
    $dataBase64 = base64_encode(file_get_contents($src));        

    $html = "<!DOCTYPE html>
    <html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <title>Visor de PDF</title>
        <style>
            html,body{
                margin: 0;
                padding: 0;
            }
            embed{
                height: 99vh;
            }
        </style>
    </head>
    <body>
        <embed src='data:application/pdf;base64,$dataBase64' width='100%' type='application/pdf'>   
    </body>
    </html>";

    echo $html;
    
asked by Renato Tapia 17.02.2018 в 06:40
source

1 answer

0

Generally this problem happens because the time it takes to execute your script exceeds the maximum default time programmed in the PHP.ini , thus causing the abrupt stop of the execution of the script. p>

To solve this you can use the set_time_limit(300); statement inside your file. This allows you to run a script (in your case uploading a larger PDF file) for a time of 300 seconds or whatever It's the same 5 minutes . If you want to place a time greater than 300 seconds you must change other parameters available in the .conf server as this response to an English StackOverflow user shows: PHP set_time_limit .

Your code would remain that way (although preferably place the set time limit at the beginning of the file .php ):

set_time_limit(300); // Esta es la línea añadida para lograr tu objetivo.
$src = urldecode($_REQUEST['archivo']);
    //$src <-- string 'Z:/327000360.pdf'

    ini_set('memory_limit', '256M');
    $dataBase64 = base64_encode(file_get_contents($src));        

    $html = "<!DOCTYPE html>
    <html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <title>Visor de PDF</title>
        <style>
            html,body{
                margin: 0;
                padding: 0;
            }
            embed{
                height: 99vh;
            }
        </style>
    </head>
    <body>
        <embed src='data:application/pdf;base64,$dataBase64' width='100%' type='application/pdf'>   
    </body>
    </html>";

    echo $html;

I hope it will be useful. A greeting!

    
answered by 17.02.2018 в 15:03