Failed to Load PDF document after downloading invoice via the API

Hello,
I am developing a Laravel Application that connects to a self hosted installation of InvoiceNinja. I am able to create, edit and send invoices comfortably using the php-sdk. However, when it comes to downloading invoices, I have totally failed. Below is the code I use to download an invoice with the php-sdk

use App\Invoice;
use InvoiceNinja\Models\Invoice as InvoiceNinjaInvoice;

----

public function download(Invoice $invoice)
{
    $invoiceNinjaFile = InvoiceNinjaInvoice::find($invoice->ninja_invoice_id);
    $pdf = $invoiceNinjaFile->download();

    header('Content-Type: application/pdf');
    header('Content-Length: '.strlen($pdf));
    header('Content-Disposition: attachment; filename="invoice_'.$invoiceNinjaFile->id.'.pdf"');
    header('Cache-Control: public, must-revalidate, max-age=0');
    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');

    echo $pdf;
}

When I run the code above, I am able to download an invoice but I notice the invoice is too small in size (<5KB) and when I try to open it with chrome, i get the error Failed to load PDF document. I am not sure what I am doing wrong. I was hoping you would assist me.

Hi,

Does the test PDF link work in the settings on Settings > Email Settings.

Snap! No it does not. It gives the same error.

The info here may help:

https://docs.invoiceninja.com/configure.html#troubleshooting

Thanks @hillel, I saw the problem and it was also because I was using an alpine docker image that I ran into more problems. I decided to give the v5 a try now that it has a stable version. I am using the direct download version (file that is 520MB) but I keep getting redirected to the setup page after successfully passing all setup steps. Any ideas?

Are there any errors in the web server logs?

Hi @hillel, I went ahead an downloaded the latest stable release, 5.1.20 on my Ubuntu VM. When the setup loads and all tests pass, I submit and it comes back to the setup form. I have checked the storage/logs but there is nothing in laravel.log.

The other issue also, is I created an account with phantomjs cloud and created an api key. However when I use that key in the .env file, the generated pdf after clicking test says:

{"name":"HttpStatusCodeException","message":"invalid Api Key. Use either 'a-demo-key-with-low-quota-per-ip-address' or create an account.   innerException: invalid api Key length of 1","stack":["no stack or extra properties unless envLevel=DEV|TEST, or logLevel=DEBUG|TRACE"]}

I’ll appreciate any guidance especially on getting past the setup screen.

If you’ve updated the .env file you may need to run php artisan optimize to update the cache

Thanks,
Now onto my setup issue. The first time I load http://localhost/ninja5/setup, I get the error below:

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = ninja and table_name = accounts and table_type = 'BASE TABLE')

Any ideas?

You may need to run php artisan migrate

1 Like

Thanks. This worked. Was able to get it up and running using Docker.

1 Like