mikehaertl / phpwkhtmltopdf

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface
MIT License
1.6k stars 238 forks source link

local paths are transformed to urls #319

Closed betz0r closed 5 years ago

betz0r commented 5 years ago

I reported this originally to wkhtml2pdf repo directly but i guess it might be an issue with this library:

https://github.com/wkhtmltopdf/wkhtmltopdf/issues/4355

I don't really find the issue why local paths, which are readable and valid, are transformed to urls.

$pdf = new mikehaertl\wkhtmlto\Pdf(array(
                    'binary' => 'PATH TO SHELLSCRIPT WITH xvfb wrapper (wkhtmltopdf.sh)',
                    'encoding' => 'UTF-8',
                    'margin-right' => 10,
                    'margin-left' => 15,
                    'margin-top' => 15,
                    'margin-bottom' => 10,
                    'title' => 'S-' . strftime('%Y-%m', $tpl_data['invoice_date_start']) . '-' . $invoice['id'],
                    'lowquality',
                    'enable-local-file-access'
                ));

wkhtmltopdf.sh contains: xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf "$@"

This is the only way i can use this library on my Ubunutu 14.04 Server originally but now it stopped working the the referenced error.

(Updated by owner to improve code example readability)

mikehaertl commented 5 years ago

Our library does not convert any HTML content in any way. All we do is to create temporary HTML files if you pass HTML content at places where wkhtmltopdf expects a URL or filename.

Also can you provide a concrete example how a local path looks in your HTML document and how it gets converted?

betz0r commented 5 years ago

Oh wow that was fast, i just updated the original post. Do you need more details?

mikehaertl commented 5 years ago

Well, as I said: How do the affected paths look in your document? Did you try to use absolute paths instead? To me it seems as if wkhtmltopdf interprets your file names as URLs. And that would not be related to our library here.

betz0r commented 5 years ago

I use codeiginter php framework to create the html files from templates and hand it to your library:

$content = $this->load->view('invoice_tpl3', $tpl_data, TRUE);
$content2 = $this->load->view('invoice_tpl4', $tpl_data2, TRUE);

$pdf = new mikehaertl\wkhtmlto\Pdf(array(
                    'binary' => 'PATH TO SHELLSCRIPT WITH xvbf router (wkhtmltopdf.sh)',
                    'encoding' => 'UTF-8',
                    'margin-right' => 10,
                    'margin-left' => 15,
                    'margin-top' => 15,
                    'margin-bottom' => 10,
                    'title' => 'S-' . strftime('%Y-%m', $tpl_data['invoice_date_start']) . '-' . $invoice['id'],
                    'lowquality',
                    'enable-local-file-access'
                ));
                $pdf->addPage($content, array());

                $pdf->addPage($content2, array(
                    'footer-center' => "[sitepage] / [sitepages]",
                    'footer-font-size' => "10",
                    'footer-spacing' => "0",
                    'header-right' => 'S-'.strftime('%Y-%m', $tpl_data['invoice_date_start']).'-'.$invoice['id'],
                    'header-center' => strftime('%d.%m.%Y', $tpl_data['invoice_date_stop']),
                    'header-font-size' => "9",
                    'header-spacing' => "2",
                ));

So as i understand you correctly, the error might be happening in the related filled templates $content and $content2 ?

betz0r commented 5 years ago

I can not find any temporary html files named "tmp_wkhtmlto_pdf_fpyCig.html" or similar in phptemp directory. I only see those files left after the process failed:

-rw-r--r--  1 censored censored 11268 May 16 09:29 file5L7yuC
-rw-r--r--  1 censored censored 11268 May 16 09:29 file66v7qn
-rw-r--r--  1 censored censored 11268 May 16 09:29 file9D3aoi
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileA406fL
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileAIT9aJ
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileavUvCY
-rw-r--r--  1 censored censored 11268 May 16 09:29 filecoZZtb
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileCwKHHv
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileeHwIv2
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileGN8y7R
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileJHcZ9x
-rw-r--r--  1 censored censored 11268 May 16 09:29 filejshZLt
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileLMaXaq
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileOS44eT
-rw-r--r--  1 censored censored 11268 May 16 09:29 filerKAYwP
-rw-r--r--  1 censored censored 11268 May 16 09:29 files5N2aC
-rw-r--r--  1 censored censored 11268 May 16 09:29 filewgSRvq
-rw-r--r--  1 censored censored 11268 May 16 09:29 filexMrz21
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileY9uS1y
-rw-r--r--  1 censored censored 11268 May 16 09:29 fileYkK9AH
betz0r commented 5 years ago

So as i dug more into this i guess this might be an issue with xvfb wrapper to use this library on headless linux server.

I use xvfb-run as virtual xserver to be able to create those pdfs on a headless linux machine.

xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf "$@"`

It creates unreadable (binary?) files in phptmp directory, so will dig into this more and report my results.

This might not be related to this library at all but it is hard to figure out where the error happens as the debugging is very limited on xvfb-run in combination with wkhtml2pdf and your library. Too much logical layers.

mikehaertl commented 5 years ago

You still should start at the obvious and check how the file paths look inside the generated HTML (i.e. $content in your case).

I'm closing this for now as I can not see any relation to our library here. If you think there's something to fix, please provide a clear boiled down example code that exemplifies the issue.