Giter Site home page Giter Site logo

nahidulhasan / laravel-pdf Goto Github PK

View Code? Open in Web Editor NEW
87.0 7.0 14.0 37 KB

A Simple package for easily generating PDF documents from HTML. This package is specially for laravel but you can use this without laravel.

PHP 100.00%
laravel pdf package html2pdf laravel-pdf pdf-generation pdf-converter laravel-html2pdf easily wkhtmltopdf

laravel-pdf's Introduction

Laravel-Html2Pdf

Latest Stable Version Total Downloads Latest Unstable Version License

A Simple package for easily generating PDF documents from HTML.This package is specially for laravel but you can use this without laravel.

Installation

Install wkhtmltopdf

This was tested on:

  • Ubuntu 14.04 x64
  • Ubuntu 16.04 x64
sudo apt-get update
sudo apt-get install xvfb libfontconfig wkhtmltopdf

For docker

RUN apt-get update && apt-get install xvfb libfontconfig wkhtmltopdf

Upddate Composer

composer require nahidulhasan/html2pdf

If laravel version < 5.5, add the ServiceProvider to the providers array in config/app.php

NahidulHasan\Html2pdf\Html2pdfServiceProvider::class,

You can optionally use the facade for shorter code. Add this to your facades:

'Pdf'  => NahidulHasan\Html2pdf\Facades\Pdf::class,

Basic Usage

To create PDF add something like this to one of your controllers.

use NahidulHasan\Html2pdf\Facades\Pdf;

$document = Pdf::generatePdf('<h1>Test</h1>');

You can also create PDF from directly calling laravel blade file. Suppose you have a mail template named greeting in view/mails folder and want to send parameter then you have to call generatePdf method as described in below

<!-- mail template stored in resources/views/mails/greeting.blade.php -->

$document =  Pdf::generatePdf(view('mails.greeting', ['name' => 'James', 'testVar' => 'demo']));

Now If you want to send mail to your client attaching pdf then you can follow this code

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->from('[email protected]')
                ->view('mails.demo')
                ->attachData($document, 'Invoice.pdf');
}
  

Download pdf

Save the PDF to a file in a specific folder, and then download it

use NahidulHasan\Html2pdf\Pdf;

$obj = new Pdf();

$html = '<html><body>'
    . '<p>Put your html here, or generate it with your favourite '
    . 'templating system.</p>'
    . '</body></html>';

$invoice = $obj->generatePdf($html);

define('INVOICE_DIR', public_path('uploads/invoices'));

if (!is_dir(INVOICE_DIR)) {
    mkdir(INVOICE_DIR, 0755, true);
}

$outputName = str_random(10);
$pdfPath = INVOICE_DIR.'/'.$outputName.'.pdf';


File::put($pdfPath, $invoice);

$headers = [
    'Content-Type' => 'application/pdf',
    'Content-Disposition' =>  'attachment; filename="'.'filename.pdf'.'"',
];

return response()->download($pdfPath, 'filename.pdf', $headers);

Other Usage

It is also possible to use the following methods :

pdf::stream('<h1>Test</h1>') Open the PDF file in browser

Running without Laravel

You can use this library without using Laravel.

Example:

use NahidulHasan\Html2pdf\Pdf;

$obj = new Pdf();
$document = $obj->generatePdf('<h1>Test</h1>');

License

Html2PDF for Laravel is open-sourced software licensed under the MIT license

laravel-pdf's People

Contributors

mahfuz05 avatar nahidulhasan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-pdf's Issues

error with lib

Call to undefined method NahidulHasan\Html2pdf\Facades\Pdf::generatePdf()

Questions

Hi thanks for the package, I was widening if your html2pdf is compatible with css files (Bootstrap 4) and @import google fonts? thanks for any help

Html2pdf ignoring css, but not on all servers

Hello. The issue is quite strange.

On a Ubuntu 22.04 hosted on google, this code:
$file = $pdf->generatePdf(view('contratto.output', [ 'contratto' => $contratto, 'allegati' => $allegati, 'preventivo' => $preventivo, 'documento_fronte' => $documento_fronte, 'documento_retro' => $documento_retro, 'cf_fronte' => $cf_fronte, 'cf_retro' => $cf_retro, 'prodotti' => $prodotti, 'cliente' => $cliente, 'isPDF' => true, ]));
works fine and generate a pdf with styles (bootstrap5 from metronic admin theme).

On a different hosted machine, same code (both update with git pull from my git repo), the pdf generated is completely without any style.

It seems that in the second case, it can't reach the external css file but this is quite absurd.

Is there any way to debug the process of creating the pdf in order to understand what's happening when loading the view?

Images don't work

No matter if I use file://, http://, or data:-urls, the resulting PDF never contains any images. It has room for them, but its only white space. How do I get images to work with this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.