Tworzenie PDF z PHP (FPDF)

0

Witam, mam problem z tworzeniem pliku pdf spod php.
Zainstalowałem sobie bibliotekę fpdf i znalazłem w sieci klasę dla pdf. Przerobiłem ją pod własne potrzeby ale teraz potrzebuję stworzyć pdf na podstawie forma w html/php i nie bardzo mogę sobie poradzić z tym.
W jednym pliczku mam kod:

 
define('FPDF_FONTPATH', dirname(__FILE__) . '/fpdf/font/');
require(dirname(__FILE__) . '/fpdf/fpdf.php');
require ('pdf.php');
$pdf = new faktura();
$pdf->Output('faktura.pdf','D');

Tworzy mi się pdf z danymi wpisanymi w klasie faktura.
Chcę zrobić form, częściowo wypełniony danymi z bazy danych i po dopisaniu paru rzeczy tworzyć pdf'a.
Nie wiem jednak jak przesłać zmienne. W pliku pdf.php jest kilka metod (m.in. Header() ) ale nie mogę przesłać żadnego argumentu.

1

Chyba za mało opisałeś swój problem, bo nie wiele z tego rozumiem co chcesz zrobić. Gdzie tu jakaś forma? Pokaż klasę faktura może?

0

Nie mam jeszcze forma ale docelowo ma być do przesyłania danych do pdf.
W klasie faktura mam na sztywno wypisanie np danych nabywcy a chciałbym je przesyłać z forma.
teraz mam:

this->Cell($this->PG_W / 5, 5,"imię nazwisko", 0, 0, 'R');

a chciałbym coś w tym stylu:

$imie=$_POST["imie"];
$nazwisko=$_POST["nazwisko"];
this->Cell($this->PG_W / 5, 5, "$imie." ".$nazwisko, 0, 0, 'R'); 

Do forma z kolei będę ładował z bazy danych z możliwością edytowania.
Nie wiem jak zmienić te dane w klasie faktura (jak je przesłać). Chyba, że coś źle kombinuję bo w sumie jestem początkujący jeśli chodzi o programowanie.

1

No mniej więcej tak jak pokazałeś. Przekazuj te dane w parametrze do metod klasy.

0

Problem w tym, że np metoda Header() jest z biblioteki fpdf i nie przyjmuje zmiennych.
Nie można przekazać zmiennej tak, żeby była dostępna we wszystkich metodach klasy?

1

Ale po cholerę chcesz modyfikować nagłówki w ogóle?
Możesz ustawić atrybuty klasy.. Ale zdaje się, że Ty w ogóle zamiast z klasy skorzystać - chcesz ją modyfikować?!

0

Chyba faktycznie źle się do tego zabieram bo na razie nie mam w klasie użytych atrybutów tylko na sztywno wpisane dane.
Chcę to właśnie przerobić ale krok po kroku zmieniając tekst w zmienne. Tylko nie wiem jak je w ogóle mam przesyłać.

Klasa wygląda tak:

 
class faktura extends FPDF {


function sizeOfText( $texte, $largeur )
{
    $index    = 0;
    $nb_lines = 0;
    $loop     = TRUE;
    while ( $loop )
    {
        $pos = strpos($texte, "\n");
        if (!$pos)
        {
            $loop  = FALSE;
            $ligne = $texte;
        }
        else
        {
            $ligne  = substr( $texte, $index, $pos);
            $texte = substr( $texte, $pos+1 );
        }
        $length = floor( $this->GetStringWidth( $ligne ) );
        $res = 1 + floor( $length / $largeur) ;
        $nb_lines += $res;
    }
    return $nb_lines;
}	
	
	function addSociete( $nom, $adresse )
{
    $x1 = 15;
    $y1 = 20;
    $this->SetXY( $x1, $y1 );
    $this->SetFont('arial_ce','B',10);
    $length = $this->GetStringWidth( $nom );
    $this->Cell( $length, 2, $nom);
    $this->SetXY( $x1, $y1 + 4 );
    $this->SetFont('arial_ce','',8);
    $length = $this->GetStringWidth( $adresse );
    $lignes = $this->sizeOfText( $adresse, $length) ;
    $this->MultiCell($length, 4, $adresse);
}

	private $PG_W = 190;

	public function __construct($passInYourDataHere = NULL) {
		parent::__construct();
		$this->Ln();
		$this->Ln();
		$this->LineItems();		
	}
	

	

	public function Header() {

		$this->AddFont('arial_ce','','arial_ce.php');
		$this->AddFont('arial_ce','I','arial_ce_i.php');
		$this->AddFont('arial_ce','B','arial_ce_b.php');
		$this->AddFont('arial_ce','BI','arial_ce_bi.php');

		
		$this->SetFont('arial_ce', 'B', 14);
		$this->Cell($this->PG_W, 8, "FAKTURA VAT Nr xxx", 0, 0, 'C');
		$this->Ln();
		
		$this->SetFont('arial_ce', 'B', 10);
		$this->Cell($this->PG_W, 5, "ORYGINAŁ", 0, 0, 'R');
		$this->Ln();
		$this->SetFont('arial_ce', 'B', 8);
		$this->Cell($this->PG_W / 4, 5,"", 0, 0, 'R');
		$this->Cell($this->PG_W / 4, 5,"", 0, 0, 'R');
		$this->Cell($this->PG_W / 4, 5,"", 0, 0, 'R');
		$this->Cell($this->PG_W / 4, 5, "Data wystawienia: ".date("d/m/Y", time()), 0, 0, 'R');
		//$this->Cell($this->PG_W / 2, 5, date("d/m/Y", time()), 0, 0, 'R');
		$this->Ln();
		$this->Ln();
		$this->Ln();	
		
		
		$this->Cell($this->PG_W / 5, 5,"", 0, 0, 'R');
		$this->Cell($this->PG_W / 5, 5,"", 0, 0, 'R');
		$this->Cell($this->PG_W / 5, 5,"", 0, 0, 'R');
		$this->Cell($this->PG_W / 5, 5,"Nabywca: ", 0, 0, 'R');
		
		
		$this->SetFont('arial_ce', '', 9);
		

		$this->MultiCell($this->PG_W / 5, 5, "imie nazwisko\nadres\nnip", 0, 'L');

		
		//$this->MultiCell($this->PG_W / 5, 5, $tekst."1 Some Road\nSome Town\nPost Code", 0, 'L');
		$this->Ln();
		$this->Ln();
		
		$this->SetFont('arial_ce', 'B', 11);
		$this->addSociete( "Nazwa firmy",
                "ADRES\n" .
                "ADRES\n".
                "ADRES\n".
				"NIP: xxxxxxxxx\n".
				"REGON: xxxxxxxx");
		$this->Ln();
		$this->Ln();		  
		$this->Ln(10);
	}
		
	public function LineItems() {
		$this->AddFont('arial_ce','','arial_ce.php');
		$this->AddFont('arial_ce','I','arial_ce_i.php');
		$this->AddFont('arial_ce','B','arial_ce_b.php');
		$this->AddFont('arial_ce','BI','arial_ce_bi.php');
		
		
		$header = array("Nazwa towaru lub usługi", "Ilość", "Cena jednostkowa netto", "Wartość netto", "VAT", "Wartość brutto");

		// Data
		
		$textWrap = str_repeat("this is a word wrap test ", 3);
		$textNoWrap = "there will be no wrapping here thank you";
		
		$data = array();
				
		$data[] = array($textWrap, 1, 50, 50, 0, 50);
		$data[] = array($textNoWrap, 1, 10500, 10500, 0, 10500);
		$data[] = array($textWrap, 1, 50, 50, 0, 50);
		$data[] = array($textNoWrap, 1, 10500, 10500, 0, 10500);
				
		/* Layout */
		
		$this->SetFont('arial_ce', 'B', 8);
		$this->AddPage();

		// Headers and widths
		
		$w = array(65, 15, 35, 25, 15, 25);

		for($i = 0; $i < count($header); $i++) {
		//$this->MultiCell($w[$i], 10, $header[$i], 1, 'C');
		$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C');
		}

		$this->Ln();

		// Mark start coords
		$this->SetFont('arial_ce', '', 8);
		$x = $this->GetX();
		$y = $this->GetY();
		$i = 0;
		
		foreach($data as $row)
		{	
			$y1 = $this->GetY();
			$this->MultiCell($w[0], 6, $row[0], 'LRB');	
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
						
			$this->SetXY($x + $w[0], $this->GetY() - $yH);
			
			$this->Cell($w[1], $yH, $row[1], 'LRB');
			$this->Cell($w[2], $yH, number_format($row[2], 2), 'LRB', 0, 'R');
			$this->Cell($w[3], $yH, number_format($row[3], 2), 'LRB', 0, 'R');
			$this->Cell($w[4], $yH, number_format($row[4], 2), 'LRB', 0, 'R');
			$this->Cell($w[5], $yH, number_format($row[5], 2), 'LRB', 0, 'R');
						
			$this->Ln();
			
			$i++;
		}
		
		$this->Ln();
		

		
		$this->setTextFont();
		$this->Cell($w[0] + $w[1], 6, 'SUMA', 'TB', 0, 'L');
		$this->SetFont('arial_ce', '', 8);
		$this->Cell($w[2], 6, number_format(2, 2), 'TB', 0, 'R');
		$this->Cell($w[3], 6, number_format(2, 2), 'TB', 0, 'R');
		$this->Cell($w[4], 6, number_format(2, 2), 'TB', 0, 'R');
		$this->Cell($w[5], 6, number_format(2, 2), 'TB', 0, 'R');
		$this->Ln();
		
		$this->setTextFont();
		$this->Ln();
		$this->Ln();
		$this->SetFont('arial_ce', 'B', 10);
		$this->Cell($this->PG_W / 4, 5,"Do zapłaty: ", 'B', 0, 'L');
		$this->Ln(10);	
		
		$this->Ln();
		$this->Ln();
		$this->Ln();
		$this->Ln();
		$this->SetFont('arial_ce', '', 8);
		$this->Cell($this->PG_W / 3, 5,"Osoba upoważniona do wystawienia faktury", 'T', 0, 'L');
		$this->Cell($this->PG_W / 3, 5,"", 0, 0, 'L');
		$this->Cell($this->PG_W / 3, 5,"Osoba upoważniona do odbioru faktury", 'T', 0, 'L');
		$this->Ln(10);	
	}

	public function Footer() {

		
		// Footer address
		
		$address = "www\nfirma\nadres\nadres\n";
		
		$this->SetY(-(($this->getAddressLength($address) * 5) + 20));

		$this->SetFont('arial_ce', '', 7);
		
		$this->Ln();
		$this->writeAddress($address);
	}

	private function setTextFont($isBold = false) {
		$this->SetFont('arial_ce', $isBold ? 'B' : '', 9);
	}
	
	private function setDataFont($isBold = false) {
		$this->SetFont('Courier', $isBold ? 'B' : '', 8);
	}

	private function getAddressLength($address) {
		return count(explode("\n", $address));
	}
		
	private function writeAddress($address) {
		$lines = explode("\n", $address);
		foreach ($lines as $line) {
			$this->Cell($this->PG_W, 5, $line, 0, 0, 'C');
			$this->Ln(4);
		}
	}	
}

2

Twoja klasa niepotrzebnie rozszerza klasę fpdf. powinno to być coś w stylu:

class faktura {
  private $fpdf = null;
  public function __construct() {
    $this->fpdf = new FPDF(); // jakies parametry byc moze?
  }

  public function header($p1, $p2, $p3) {
    $this->fpdf->jakasMetoda($p1);
    $this->fpdf->cosInnego($p2, $p3);
    if ($p1 > 10) {
      $this->fpdf->innaMetoda();
    }
  }

  public function costam($dane) { /* ... */ }

i potem w Twoim "głównym" kodzie:

$faktura = new faktura();
$faktura->header($dane_z_bazy1, $dane_z_bazy2, $dane_z_bazy3);
foreach ($dane_z_bazy666 as $dane) {
  $faktura->costam($dane);
}
// itd.
0

Ok, przerobiłem kod na ten wzór ale teraz nie tworzy mi się w ogóle ten nagłówek.
Zmienne mogę już przesyłać jakby co.

Edit:
Ok, brakowało
$this->fpdf->AddPage();
w header.

1 użytkowników online, w tym zalogowanych: 0, gości: 1