phpcakephpwkhtmltopdf

Passing a URL to CakePdf


Using CakePHP 2.6.7

When generating a PDF using wkhtmltopdf I can simply run from the command line wkhtmltopdf http://url/of/my/website some_name.pdf but I can find no way to pass a URL in this manner through CakePdf when using the Wkhtmltopdf engine. The closest I can get is using file_get_contents(http://my/website) and then manually going through the result and turning relative URLs for stylesheets/scripts into full URLs.

Is there a way to pass a URL to CakePdf? Alternatively, what would be the best way to go through a load of HTML and turn the relative links into full ones?

Partial Solution

For the moment I have managed to use the following code to manipulate the links on the page and replace them with the full urls.

$parsed_web_address = parse_url($this->request->data['ArmawareHtmlToPdf']['web_address']);
$root_web_address = $parsed_web_address['scheme'] . '://' . $parsed_web_address['host'] . '/';

$html_string = str_replace('href="/', 'href="' . $root_web_address, $html_string);
$html_string = str_replace('src="/', 'src="' . $root_web_address, $html_string);

Solution

  • No. As far as I'm aware you can't pass a URL through CakePdf. The entire purpose of the plugin is to convert html generated from your Cakephp app to wkhtmltopdf. So it wouldn't make much sense to pass a url to a external html source.

    My suggestion would be to use wkhtmltopdf directly using proc_open(). Take a look at the WkHtmlToPdfEngine.php file in the CakePdf plugin for an example of using this with wkhtmltopdf.

    http://php.net/manual/en/function.proc-open.php