Save php output to PDF

Question!

I have a script that opens a print dialogue to output some generated information that was styled with css. With this dialogue I can print to PDF. Is there a way to convert this output instantly to PDF?

I already tried some API web service like html2pdf, but these services generate only blank pages, because the output gets directly send to printing dialogue. Is there a way around this? (without styling my infos completely new with tcpdf / fpdf etc.)

Example: http://dev.prettynormal.de/index.php?task=productprint&pid=1480&_wpnonce=0f275205f2

By : fourgood


Answers

use FPDF which is very easy to use;

require('fpdf.php');
 class PDF extends FPDF{
function header()//create header of the pdf
                {

                    $this->Image('Your Logo url', 10, 6, 90);
                    $this->Ln(30);
                    $this->SetFont('Arial', 'B', 15);
                    // Move to the right
                    $this->Cell(50);
                    // Title
                    // $this->Cell(90, 110, 'User Website Brief', 'C');
                    // Line break
                    $this->Ln(20);
                }
function footer()
                {

                    $this->SetY(-15);
                    // Arial italic 8
                    $this->SetFont('Arial', 'I', 8);
                    // Page number
                    $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
                }

            }
$pdf = new pdf();
$pdf->AliasNbPages();
            $title ='Your PDF Title';
            $pdf->SetTitle($title);
            $pdf->SetAuthor('Your Name');
            $pdf->AddPage();
            $pdf->SetFont('Arial', 'B', 14);
$pdf->cell(20, 10, $YourContent);
$pdf->Output('I',"FileName");//sends PDF output to the browser

You can download FPDF here If it gives you problems please past your code so I can assist you.



Another way to do that is by means of a trailing return type as the following one:

auto func(int i) -> decltype(void(i)) {}
int main() {}

If you have more than one variable, you can list them all:

auto func(int i, int j) -> decltype(void(i), void(j)) {}
int main() {}

And you can still declare your preferred return type if void is not what you want:

auto func(int i) -> decltype(void(i), int{}) { return 42; }
int main() {}

The advantages of this solution are:

  • Variable name is preserved: as mentioned by others, do not give a name to the variable could not be an option (because of your documentation system, as an example).

  • You won't pollute your function body with useless expressions aimed to silent a few warnings.

  • You don't have to explicitly define support function to do that.

Of course, this doesn't apply to static variables declared in the function body, but you can do something similar when returning from the function (just an example):

int f() {
    static int i = 0;
    static int j = 0;
    return void(i), void(j), 42;
}

int main () {}

More ore less the same advantages.

By : skypjack


std::ignore was not intended to be used for this purpose:

An object of unspecified type such that any value can be assigned to it with no effect. Intended for use with std::tie when unpacking a std::tuple, as a placeholder for the arguments that are not used.


I would suggest you not do what you are thinking, since in a real-world big project, it will lead to code that is harder to maintain, where one would look at the prototype of a function, would see that it takes an argument int i, but the function would not need that in reality - doesn't feel nice, does it? :)

By : gsamaras


This video can help you solving your question :)
By: admin