# Contributte\PdfResponse

# Content

# Usage

# How to prepare PDF from template

use Contributte\PdfResponse\PdfResponse;

// in a Presenter
public function actionPdf()
{
	$template = $this->createTemplate();
	$template->setFile(__DIR__ . "/path/to/template.latte");
	$template->someValue = 123;
	// Tip: In template to make a new page use <pagebreak>

	$pdf = new PdfResponse($template);

	// optional
	$pdf->documentTitle = date("Y-m-d") . " My super title"; // creates filename 2012-06-30-my-super-title.pdf
	$pdf->pageFormat = "A4-L"; // wide format
	$pdf->getMPDF()->setFooter("|© www.mysite.com|"); // footer

	// do something with $pdf
	$this->sendResponse($pdf);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# Save file to server

use Contributte\PdfResponse\PdfResponse;

public function actionPdf()
{
	$template = $this->createTemplate();
	$template->setFile(__DIR__ . "/path/to/template.latte");

	$pdf = new PdfResponse($template);

	$pdf->save(__DIR__ . "/path/to/directory"); // as a filename $this->documentTitle will be used
	$pdf->save(__DIR__ . "/path/to/directory", "filename"); // OR use a custom name
}
1
2
3
4
5
6
7
8
9
10
11
12

# Attach file to an email

use Contributte\PdfResponse\PdfResponse;

public function actionPdf()
{
	$template = $this->createTemplate();
	$template->setFile(__DIR__ . "/path/to/template.latte");

	$pdf = new PdfResponse($template);

	$savedFile = $pdf->save(__DIR__ . "/path/to/directory");
	$mail = new Nette\Mail\Message;
	$mail->addTo("john@doe.com");
	$mail->addAttachment($savedFile);
	$mailer = new SendmailMailer();
	$mailer->send($mail);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# Force file to download

use Contributte\PdfResponse\PdfResponse;

public function actionPdf()
{
	$template = $this->createTemplate();
	$template->setFile(__DIR__ . "/path/to/template.latte");

	$pdf = new PdfResponse($template);
	$pdf->setSaveMode(PdfResponse::DOWNLOAD); //default behavior
	$this->sendResponse($pdf);
}
1
2
3
4
5
6
7
8
9
10
11

# Force file to display in a browser

use Contributte\PdfResponse\PdfResponse;

public function actionPdf()
{
	$template = $this->createTemplate();
	$template->setFile(__DIR__ . "/path/to/template.latte");

	$pdf = new PdfResponse($template);
	$pdf->setSaveMode(PdfResponse::INLINE);
	$this->sendResponse($pdf);
}
1
2
3
4
5
6
7
8
9
10
11

# Set a pdf background easily

use Contributte\PdfResponse\PdfResponse;

public function actionPdf()
{
	$pdf = new PdfResponse('');
	$pdf->setBackgroundTemplate(__DIR__ . "/path/to/an/existing/file.pdf");

	// to write into an existing document use the following statements
	$mpdf = $pdf->getMPDF();
	$mpdf->WriteFixedPosHTML('hello world', 1, 10, 10, 10);

	// to write to another page
	$mpdf->AddPage();

	// to move to exact page, use
	$mpdf->page = 3; // = move to 3rd page

	$this->sendResponse($pdf);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Create pdf with latte only

use Contributte\PdfResponse\PdfResponse;

public function actionPdf()
{
	$latte = new Latte\Engine;
	$latte->setTempDirectory('/path/to/cache');
	$latte->addFilter('money', function($val) { return ...; }); // formerly registerHelper()

	$latte->onCompile[] = function($latte) {
		$latte->addMacro(...); // when you want add some own macros, see http://goo.gl/d5A1u2
	};

	$template = $latte->renderToString(__DIR__ . "/path/to/template.latte");

	$pdf = new PdfResponse($template);
	$this->sendResponse($pdf);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Configuration of custom temp dir for mPDF in PdfResponse

services:
	- Contributte\PdfResponse\PdfResponseFactory([tempDir: %tempDir%/pdf])
1
2

and in your PHP code:

use Contributte\PdfResponse\PdfResponse;

public function __construct(
	private readonly PdfResponseFactory $pdfResponseFactory
)
{
}

public function actionPdf()
{
	$template = $this->createTemplate();
	$template->setFile(__DIR__ . "/path/to/template.latte");

	$response = $this->pdfResponseFactory->createResponse();
	$response->setTemplate($template);
	$response->setSaveMode(PdfResponse::INLINE);

	$this->sendResponse($response);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19