Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP example #18

Open
michcald opened this issue Feb 29, 2016 · 3 comments
Open

PHP example #18

michcald opened this issue Feb 29, 2016 · 3 comments

Comments

@michcald
Copy link

Thought it might be useful for someone to see a PHP example.

class PdfGenerator {

private $serviceUrl;

private $servicePort;

// ...

private function getHtml()
{
  // ... generates the HTML to convert to PDF
}

private function generatePdf()
{
        $data = array(
            'contents' => base64_encode($this->getHtml($loan)),
            'options' => array(
               // ...
            ),
        );
        $dataString = json_encode($data);
        $headers = array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($dataString),
        );

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $this->serviceUrl);
        curl_setopt($ch, CURLOPT_PORT, $this->servicePort);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
}
}
@nazar-pc
Copy link

There is much easier way to do it without curl:

$context = stream_context_create(
	[
		'http' => [
			'method'  => 'POST',
			'header'  => 'Content-Type: application/json',
			'content' => json_encode(
				[
					'contents' => base64_encode('<h1>html content here</h1>')
				]
			)
		]
	]
);
$pdf     = file_get_contents("http://127.0.0.1:1234", null, $context);

@cve
Copy link

cve commented Sep 27, 2018

pdf is created, but images does not display

@vicluber
Copy link

I adition to nazar-pc comment, you can send wkhtmltopdf options like this.

$context = stream_context_create( [ 'http' => [ 'method' => 'POST', 'header' => 'Content-Type: application/json', 'content' => json_encode( [ 'contents' => base64_encode($htmlStringView), 'options' => [ 'page-size' => 'A4', 'footer-right' => '[page]', 'margin-bottom' => '20', 'margin-top' => '20', 'footer-font-size' => '8' ] ] ) ] ] ); $pdf = file_get_contents("http://laradock_wkhtmltopdf_1/", null, $context);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants