Forum How do I...?

Trouble generating a PDF

sijo
// your code to generate invoice HTML goes here
$out = '<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
This is Test
</body>
</html>';

require_once(__DIR_LIBRARY."/princexml/prince.php");
$prince = new Prince('/usr/bin/prince');

$prince->setHTML(true);
$prince->setLog('/tmp/prince.log');

header('Content-type: application/pdf');

$prince->convert_string_to_passthru($out);

Geting error like "This pdf document might not be displayed correctly"

any help....its urgent
mikeday
Can you save the PDF output as a local file, then check it with a text editor especially at the beginning or end, to see if some HTML is there? Often the PHP is adding unwanted HTML to the output.
sijo
Hi mike,

how can i save a PDF output as a local file by code? please help..

thanks

Edited by sijo

mikeday
You can just use "Save Page As..." in Firefox, or change your PHP to use convert_string_to_file() and specify a filename on the server where your PHP code has write permissions.
sijo
Hi,

$out = '<html>
<head>
</head>
<body>
This is Test
</body>
</html>';

require_once(__DIR_LIBRARY."/princexml/prince.php");
$prince = new Prince('/usr/bin/prince');

$prince->setHTML(true);
$prince->setLog('/tmp/prince.log');

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="foo.pdf"');

$prince->convert_string_to_passthru($out);

mike, am using the above code for PDF generation using princexml and getting an empty file, when it 'save as' text file as attachment.

thanks for help
  1. foo.pdf0.0 kB

Edited by sijo

mikeday
Is /tmp/prince.log being created?
sijo
Hi,

Yes, its creating mike and am also attaching the same...
  1. prince.log10.2 kB
mikeday
This line indicates the problem:
Mon Mar 24 11:40:08 2014: internal error: no available fonts

You will need to install some fonts, either the Microsoft Core Fonts, or the DejaVu fonts.
sijo
Mike, can you please explain how we install fonts in Linux server, am using the Linux systems.
mikeday
It depends what Linux distribution you are running. You can check this by running "lsb_release -a" or checking /etc/redhat-release or /etc/centos-release, or asking your sysadmin.
sijo
Mike, you have any sample code for PDF generation with out using fonts. For now our sys admin checking to install the fonts mentioned by you.

thanks...
sijo
Hi, we are running on 'Centos' Linux distribution. so now you can provide the font installation steps. Our sys admin said, he installed all fonts but now also in log file showing 'internal error: no available fonts' error.

thanks for help...
mikeday
It's probably best to run Prince manually from the command-line when testing, and try under PHP when that is working. If you run "fc-list | grep Arial" or "fc-list | grep DejaVu" does it show up any fonts?
sijo
Mike, Thank you very much for the help, its working fine now and that fonts installed correctly, then PDF generates well. thanks...:)

Edited by sijo

sijo
Hi, when PDF generates i noted mainly two things:

1. first page of PDF contains 'princexml' logo, can we remove that?
2. the whole page content shrink to less (one page showing two pages now), can we make it full width?

Thanks for your time...

Edited by sijo

mikeday
The logo can be removed by purchasing a license. Regarding the width, you may want to reduce the page margins:
@page {
    size: A4;
    margin: 1cm
}

However, paper is normally narrower than a browser window, so if your HTML has a big fixed width, it may not fit well.
sijo
Thanks mike, your reply makes perfect and you have any Skype id to chat because i would like to buy a paid version of product and some more comments. thanks...
mikeday
I don't use Skype actually, but you can email me (mikeday@yeslogic.com).