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

Special chars in header and footer #106

Closed
mathieumuller opened this issue Jul 17, 2015 · 23 comments
Closed

Special chars in header and footer #106

mathieumuller opened this issue Jul 17, 2015 · 23 comments

Comments

@mathieumuller
Copy link

Hi,
I am trying to generate a pdf with an header containing french special chars (é, è, à...)using the 'header-left' or 'header-right' options. To make sure that my document is right encoded I've added the 'encoding' option set to 'UTF-8' but my special chars are not displayed at all in my header. Does anyone have an idea?
Thanks in advance...

@micayael
Copy link

Same problem here. The content prints correct accents but texts into the header and footer removes those characters.

I print the content of $command variable in "protected function executeCommand($command)" (Knp\Snappy\AbstractGenerator) and looks right:

/usr/local/bin/wkhtmltopdf --dpi '300' --grayscale --orientation 'Portrait' --page-size 'A4' --default-header --encoding 'UTF-8' --footer-font-size '7' --footer-left 'Fecha de Impresión [isodate] [time]' --footer-line --footer-right 'Página [page]  de [toPage]' --footer-spacing '0' --header-font-size '7' --header-left 'Código: {codigo}' --header-line --header-right 'Fecha de Emisión: {fecha_emision}' --header-spacing '5' --viewport-size '1024x768' '/tmp/knp_snappy55d20da5cdf7c7.79424044.html' '/tmp/knp_snappy55d20da5d1bbd1.98619814.pdf'

If I execute this code from console works but from the browser remove characters with accents. I am using php-fpm and apache.

Maybe is a requirement we are missing into some configuration into this bundle or KnpLabs/snappy.

@akovalyov
Copy link
Contributor

hm OK, I thing I got the issue.
@micayael could you try to set

setlocale(LC_CTYPE, "your_LOCALE.UTF-8");
//OR
setlocale(LC_ALL, "your_LOCALE.UTF-8");
```?
and try it again?
And also type ```locale``` in your shell and paste content here, please.

@micayael
Copy link

Same result using setlocale(LC_CTYPE, "en_US.UTF-8") and setlocale(LC_ALL, "en_US.UTF-8"). I try it within web/app_dev.php and within executeCommand() method, before execute the command.

I got this result executing "locale" on my terminal:

[root@798e56631c3b /]# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

@akovalyov
Copy link
Contributor

@micayael could you try es_PY locale (I hope, it is correct for Paraguay :))?

@micayael
Copy link

Thanks for your reply @akovalyov

I do this on RHEL7

localedef -v -c -i es_PY -f UTF-8 es_PY.UTF-8
export LANG=es_PY.utf8

And now I have this:

[root@0024ac5f6a7a /]# locale
LANG=es_PY.utf8
LC_CTYPE="es_PY.utf8"
LC_NUMERIC="es_PY.utf8"
LC_TIME="es_PY.utf8"
LC_COLLATE="es_PY.utf8"
LC_MONETARY="es_PY.utf8"
LC_MESSAGES="es_PY.utf8"
LC_PAPER="es_PY.utf8"
LC_NAME="es_PY.utf8"
LC_ADDRESS="es_PY.utf8"
LC_TELEPHONE="es_PY.utf8"
LC_MEASUREMENT="es_PY.utf8"
LC_IDENTIFICATION="es_PY.utf8"
LC_ALL=

But still not working.

Anyway, the weird thing is that the output command is right:

/usr/local/bin/wkhtmltopdf --dpi '300' --image-dpi '600' --image-quality '100' --margin-left '20mm' --margin-right '5mm' --margin-top '30mm' --orientation 'Portrait' --page-size 'A4' --encoding 'UTF-8' --footer-font-size '7' --footer-left 'Fecha de Impresión [isodate] [time]' --footer-line --footer-right 'Página [page]  de [toPage]' --footer-spacing '0' --header-font-size '7' --header-html 'http://10.128.24.30:9091/header.html' --header-left '[section]' --header-right '' --header-spacing '5' --viewport-size '1024x768' '/tmp/knp_snappy55d37e582cdc39.49113271.html' '/tmp/knp_snappy55d37e582ce3a3.87932398.pdf'

When I execute from terminal the PDF is correct but from browser removes the accent characters

  • Fecha de Impresin 2015-08-18 14:52:14
  • Pgina 1 de 1

@micayael
Copy link

Is there any solution for this?

@pilot
Copy link
Contributor

pilot commented Sep 22, 2015

seems such issue come from there https://github.com/KnpLabs/snappy/blob/e3364520431fcce40f3b5051630fd97c0e08b15b/src/Knp/Snappy/AbstractGenerator.php#L428

escapeshellarg was escape all non-ASCII characters from a UTF-8 string

try to add setlocale(LC_CTYPE, "en_US.UTF-8");

@mathieumuller
Copy link
Author

Hi guys, I don't know if this solution can fix with your issues but I prefered the the header-html and footer-html options to the header and footer ones. If it could help...

@micayael
Copy link

@mathieumuller how do you use page numbers configuration within the html? (footer-right: 'Página [page] de [toPage]'). Because is there where I use accents.

@mathieumuller
Copy link
Author

@micayael below is an example of how I use the page numbers in the footer-html according to the wkhtmltopdf doc. As you can see, you have to use a little js script to fill html elements representing your [page] and [toPage] values.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,300' rel='stylesheet' type='text/css'>
    </head>

    <body id="body" style='width:100%; font-size:0.8em; margin-top:0px; margin-bottom:10px; font-family: "Open Sans", sans-serif;'>
        <div style='text-align:right'>
            <span id='pdfkit_page_current'></span>/<span id='pdfkit_page_count'></span>
        </div>
        <script>
            var pdfInfo = {};
            var x = document.location.search.substring(1).split('&');
            for (var i in x) { var z = x[i].split('=',2); pdfInfo[z[0]] = unescape(z[1]); }
            function getPdfInfo() {
                var page = pdfInfo.page || 1;
                var pageCount = pdfInfo.topage || 1;
                document.getElementById('pdfkit_page_current').textContent = page;
                document.getElementById('pdfkit_page_count').textContent = pageCount;
            }
            getPdfInfo();
        </script>
    </body>
</html>

@micayael
Copy link

@mathieumuller thanks. It works

@pilot I have this values

¨¨¨
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

and I try to use also setlocale(LC_CTYPE, "en_US.UTF-8"); within the controller for example but its the same.

The LC_ALL variable when I execute "locale" on the terminal sould be nothing? "LC_ALL="

@akovalyov
Copy link
Contributor

@micayael what server do you use? What default charset is set for the webserver?

@micayael
Copy link

@akovalyov , thanks for reply.

I use a docker with RHEL7.1, php-fpm and apache.

# locale -a
C
en_US.utf8
POSIX
# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

The default chartset that you said sould be from apache or php-fpm?

@akovalyov
Copy link
Contributor

@micayael could you add IndexOptions +Charset=UTF-8 to your .htaccess file and retry?

@micayael
Copy link

@akovalyov I add it but is the same, no accents. Could be php-fpm configuration?

@akovalyov
Copy link
Contributor

@micayael could you share your docker/docker-compose config so that I can take a look? I am really interested to find and resolve the issue since we don't really have an answer for this particular trouble and I'd be happy to finally resolve it. If you don't want to share config here - you can drop me a line on the email you can find in my profile ;)

@micayael
Copy link

@akovalyov thanks. I just sent you the dockerfile via email. Let me know if you need something else. I am really interested too.

@akovalyov
Copy link
Contributor

OK, finally I have a possible solution for this issue.
@micayael @mathieumuller can you check the note I have added in https://github.com/KnpLabs/snappy/pull/194/files#diff-2e69fdb5c4abb5fedfcbdb7acbc2c915R66 and tell if it works for you?
Waiting for your input.

@gonzaloplaza
Copy link

Hi, we're experiencing same issue with 'UTF-8' chars using PDF export function. We have tried all solutions you commented but we're still having this issue.

I'm almost sure its related to server locale configuration. I read this about 'escapeshellarg' command
http://php.net/manual/es/function.escapeshellarg.php

"If escapeshellarg() function removes your accents (like á, a with an 'accute') from the given string, ensure your LC_ALL variable is correct. If using it via web, you need to restart Apache or the corresponding web server after setting LC_ALL with a export LC_ALL=es_ES.utf8 (for example) from your shell."

We're using ubuntu 12.04 with es_ES.UTF-8 encoding

@akovalyov
Copy link
Contributor

@gonzaloplaza what does locale -a show?

@akerouanton
Copy link
Contributor

I know this is a recurring issue but without more details about installed locales, value of LC_* env vars, execution context ... this is pretty much impossible to reproduce and debug. I closes this issue due to inactivity, but if you face the same problem, please open a new issue and ping me.

@vish4395
Copy link

I also facing this issue on ubuntu

@coolskin2b
Copy link

coolskin2b commented Dec 5, 2022

I found the solution for footer and header, when you create your pdf object add the local like this :

			$pdf = new Pdf(
				array(
					'orientation' => ($paysage > 0) ? 'landscape' : 'portrait', //   -O, --orientation <orientation>   
					'footer-html' => 'https://'.$footer_page, // --footer-right "Page [page] of [toPage]"
					'footer-font-size' => '8', //  --footer-font-size 8
					'commandOptions' => array(
						'procEnv' => array(
							// Check the output of 'locale' on your system to find supported languages
							'LANG' => 'en_US.utf-8', // this line do the local change for display accents and spécials characters
						),
					),
				)
			);

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

8 participants