ilovepdf / ilovepdf-php

iLovePDF Rest Api - PHP Library (https://developer.ilovepdf.com)
147 stars 39 forks source link

Calling the download method on an object only downloads the file to the local directory. #7

Closed SteSher closed 6 years ago

SteSher commented 6 years ago

Hi Calling the download method on an object only downloads the file to the local directory rather than downloading to the users browser/computer. I tried using readFile() but it doesn't seem to work. Am I missing something? Here's my code, everything works and I have no issues with anything except for the download.

<!DOCTYPE html>
<html>
    <body>
        <form action="pdf-to-jpg.php" method="post" enctype="multipart/form-data">
            Select image to upload:
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload PDF" name="submit">
        </form>
    </body>
</html>

<?php
require_once('ilovepdf/init.php');
use Ilovepdf\Ilovepdf;
$upload_directory = 'uploads/';
$upload_name = $_FILES['fileToUpload']['name'];
$upload_size = $_FILES['fileToUpload']['size'];
$upload_type = $_FILES['fileToUpload']['type'];
$upload_tmp = $_FILES['fileToUpload']['tmp_name'];
$upload_file = $upload_directory.$upload_name;
move_uploaded_file($upload_tmp, $upload_file);
try
{
    $ilovepdf = new Ilovepdf('project_public_bc71e476a92d1e98d8f30662523d605b_K1puUe100e70c76ef01f70cae1da3587dcbf8','secret_key_27d318df13715db4ec56bf66f4baaf00_Vdu4j2e7fb8c79ef537a7aebad4ba02208d55');
    $myTaskConvertPDF = $ilovepdf->newTask('pdfjpg');
    $file = $myTaskConvertPDF->addFile(dirname(__FILE__)."/".$upload_file);
    $myTaskConvertPDF->execute();
    $myTaskConvertPDF->download();
}
catch (\Ilovepdf\Exceptions\StartException $e) 
{
    echo "An error occured on start: " . $e->getMessage() . " ";
}
catch (\Ilovepdf\Exceptions\AuthException $e) 
{
    echo "An error occured on auth: " . $e->getMessage() . " ";
    echo implode(', ', $e->getErrors());
} 
catch (\Ilovepdf\Exceptions\UploadException $e) 
{
    echo "An error occured on upload: " . $e->getMessage() . " ";
    echo implode(', ', $e->getErrors());
} 
catch (\Ilovepdf\Exceptions\ProcessException $e) 
{
    echo "An error occured on process: " . $e->getMessage() . " ";
    echo implode(', ', $e->getErrors());
} 
catch (\Ilovepdf\Exceptions\DownloadException $e) 
{
    echo "An error occured on process: " . $e->getMessage() . " ";
    echo implode(', ', $e->getErrors());
} 
catch (\Exception $e) 
{
    echo "An error occured: " . $e->getMessage();
}
?>
maztch commented 6 years ago

Use the toBrowser method after download.

$myTaskConvertPDF->toBrowser();

This is not currently documented on developer.ilovepdf.com.

On next version this method will not require to save locally the file first (download method will not be required).

SteSher commented 6 years ago

I inserted the line: $myTaskConvertPDF->toBrowse(); after $myTaskConvertPDF->download(); but I received a fatal error. Fatal error: Call to undefined method Ilovepdf\PdfjpgTask::toBrowse()

marcogrossisas commented 6 years ago

Hello @SteSher please try: $myTaskConvertPDF->toBrowser(); adding a final 'r'

SteSher commented 6 years ago

Okay that's embarressing. Thanks for that.