anam's avatar
Level 3

HTML to PDF/Image conversion

I have developed a package which provides a simple API to ease the process of converting HTML to PDF or images.

https://github.com/anam-hossain/phantommagick

This package is new and looking for reviews to improve the API. Any thoughts would be appreciated.

Thank you in advanced.

0 likes
42 replies
sarathiscookie's avatar

Hi anam, I have used your package. But my project showing "Exception in Runner.php line 164: Binary does not exist" error.

routes.php

Route::post('/phantommagick', function(){
 // base_path() is //"C:\xampp\htdocs\careertag"
    $conv = new \Anam\PhantomMagick\Converter();
    $conv->make('http://google.com')
        ->setBinary(base_path() . "/phantomjs-2.0.0-windows/bin/phantomjs.exe")
        ->toPdf()
        ->download('google.pdf');

});

detailed error

 in Runner.php line 164
at Runner->pickBinary() in Runner.php line 100
at Runner->run('C:\xampp\htdocs\careertag\vendor\anam\phantommagick\src/scripts/phantom_magick.js', 'http://google.com', 'C:\Users\user-pc\AppData\Local\Temp/1538756026716c9129.pdf', array('format' => 'A4', 'zoomfactor' => '1', 'quality' => '70', 'orientation' => 'portrait', 'margin' => '1cm')) in Converter.php line 619
at Converter->saveLocal('C:\Users\user-pc\AppData\Local\Temp/1538756026716c9129.pdf') in Converter.php line 595
at Converter->save('C:\Users\user-pc\AppData\Local\Temp/1538756026716c9129.pdf') in Converter.php line 528
at Converter->download('google.pdf') in routes.php line 51
at RouteServiceProvider->{closure}()
1 like
jaredmeakin's avatar

Hey @anam,

I'm using your package to generate PDF printouts. It's working great for me. Still playing with the CSS to get everything looking just right, but all-in-all a great package!

One question/problem I am facing is when I try to generate a PDF for a protected route. If I do this it simply creates a PDF of the app's login page. How could I go about "authenticating" the convertor before it tries to access the URL?

If you could point me in the right direction I'd really appreciate it! Thanks!

anam's avatar
Level 3

@jaredmeakin PhantomJS uses their own WebKit to process the HTTP request and the Laravel session is not available in command line. So, the authentication will fail when the route hit through PhantomJS.

However, I can suggest you one method to get around this. Instead of the passing a secure route to PhantomMagick, you can generate the view in a controller. This will allow you to verify an authenticated user.

public function download(User $user)
{
  // Render view in controller
   $html =  View::make('invoices.payment', compact('user'))->render();

   $conv = new Converter();
   $conv->addPage($html)
    ->save('/your/destination/path/invoice.pdf');
}

The above operation will generate raw html. so it has some limitations.

PhantomMagick only supports HTML and data can be provided via an URL or from the local disk. If you need to use raw HTML data, you can use multipage PDF conversion. However raw data does have some limitations; it does not support relative paths and it only supports inline styles and internal CSS.

More details: https://github.com/anam-hossain/phantommagick#data-source

1 like
jaredmeakin's avatar

Thanks! Love your package. I couldn't get the the make method on view to work.

However, I was able to work around this by using a custom middleware to replace 'auth'. It's a great setup now that I have the Event, Listener, and Queue working. I'm able to dynamically create hundreds of PDFs on a single call. Thanks!

selimppc's avatar

@Anam This one is awesome. But I got something is missing in my application. Can I get some guide from you ?

My method :

    $bg_path = public_path()."/tickets/ticket_bg.jpg";
    $options = [
        'width' => 500,
        'height' => 300,
        'quality' => 90
    ];

        $conv = new  \Anam\PhantomMagick\Converter();

        $tnm= $ticket->ticket_number;
        $ticket->ticket_number= substr($ticket->ticket_number,0,-4).'****';
        $conv->addPage(TicketController::ticket_html($ticket))
            ->setImageOptions($options)
            ->toJpg()
            ->save(public_path().'/assets/tickets/P-'.$tnm.'.jpg');
    }

this one is not showing any error .. But doesn't store any file into my local folder . May I need to do anything more ?

Note : install packages and added into app.php file according to your instruction. I checked the libraries / dependencies are working.

When I print_r($conv) at last its showing this one : http://prntscr.com/cyupr8 (screenshot)

1 like
anam's avatar
Level 3

@selimppc In which operating system your app running on?

Most of the cases, these errors are related to wrong PhantomJs library installed to the system.

selimppc's avatar

@Anam :

I use os :

  1. Mac OSX
  2. Ubuntu 16
  3. Hostgator Linux .

I have tried all them it doesn't work. But when I install PhantomJs in OS X using brew and larval 5.3 it works.

Would you please guide me for installing PhantomJs using Composer or others ways ?

anam's avatar
Level 3

@selimppc I can see from screenshot, you are using phantomjs-linux-x86-binary which is for 64bit Linux OS. I recommend you to use Ubuntu server with https://github.com/anam-hossain/phantomjs-linux-x86-binary binary.

In addition, you should start over (Install homestead again) and follow the following steps:

Step1: Install homestead

Step2: Homestead ssh and cd to you app.

Run composer require anam/phantommagick

Step 3: Run composer anam/phantomjs-linux-x86-binary. Make sure you are in ubuntu server.

Step 4: Try the following:

$conv = new \Anam\PhantomMagick\Converter();
$conv->source('http://google.com')
    ->toPdf()
    ->save('/your/destination/path/google.pdf');
bhmantan's avatar

@jaredmeakin hi Jared, could you elaborate more about your "custom middleware to replace 'auth' " solution please?

I am using laravel and vue js on my project, need to use converter to generate and image. And right now I am stuck because the auth problem.

zachsnaider's avatar

Thanks for this package! I need to compare it with another and decide what is better for me since I already used API from htmlpdfapi.com earlier for my project on Assignment.EssayShark. I will not say that it is more convenient, but I'm still working with it.

elizabethdavid's avatar

I use your package to generate PDF printouts. It is good to me. Still playing with css and making everything look exactly right, but all in one great package! And with the other one, and deciding what is better because I've used the API early on my project to write my essay paper. I will not say it is more convenient, but I am still using it.

komalbhatt's avatar

Finally I got is solve by using setbinary() for custom phantomjs

furmanalinb's avatar

@anam Chinese text in html is not converting to the image? Any multilingual support option ?

alishakihn's avatar

Hello anam, I have used your package. I feel this new package is quite stable, if I find any errors I will notify you. Flappy Bird

alicentjenner's avatar

Converting HTML to PDF/Image facilitates document sharing and presentation versatility.

dryfirewood's avatar

This is a good opportunity for you to indulge yourself in rhythm games by clicking . it's great!

Steck's avatar

I have learned some other interesting knowledge and even some games. Thank! slopegame.io

Please or to participate in this conversation.