25if (!defined(
'NOTOKENRENEWAL')) {
26 define(
'NOTOKENRENEWAL', 1);
28if (!defined(
'NOCSRFCHECK')) {
29 define(
'NOCSRFCHECK', 1);
32require
'../../main.inc.php';
33require_once DOL_DOCUMENT_ROOT .
'/core/lib/pdf.lib.php';
34require_once DOL_DOCUMENT_ROOT .
'/core/lib/date.lib.php';
35require_once DOL_DOCUMENT_ROOT .
'/core/lib/functions2.lib.php';
36require_once DOL_DOCUMENT_ROOT .
'/includes/tecnickcom/tcpdf/tcpdf.php';
44$langs->loadLangs(array(
'products',
'stocks',
'suppliers',
'companies',
'margins',
'reports@reports'));
47$json =
GETPOST(
'data',
'restricthtml');
49 $json = file_get_contents(
'php://input');
53$data = json_decode($json,
true);
54if (json_last_error() !== JSON_ERROR_NONE) {
55 dol_syslog(
"Invalid JSON data in PDF generation: " . json_last_error_msg(), LOG_ERR);
56 print
"Error: Invalid data format provided.";
61$title =
GETPOST(
'title',
'restricthtml');
66$filename =
GETPOST(
'filename',
'restricthtml');
67if (empty($filename)) {
68 $filename =
'Report.pdf';
72$filename = preg_replace(
'/[\x00-\x1F\x7F\/\\:*?"<>|]/',
'', $filename);
74if (!preg_match(
'/\.pdf$/i', $filename)) {
79 print
"No data provided for PDF generation.";
84 $outputlangs = $langs;
89 $pdf_dimensions = [$format[
'width'], $format[
'height']];
92 $pdf->SetCreator(
"Dolibarr AI");
93 $pdf->SetAuthor($user->getFullName($langs));
95 $pdf->SetTitle($title);
96 $pdf->SetMargins(15, 15, 15);
97 $pdf->SetAutoPageBreak(
true, 15);
98 if (class_exists(
'TCPDF')) {
99 $pdf->setPrintHeader(
false);
100 $pdf->setPrintFooter(
false);
103 $pdf->SetFont(
'dejavusans',
'', 10);
106 $pdf->SetFont(
'',
'B', 16);
107 $pdf->Cell(0, 10, $title, 0, 1,
'C');
108 $pdf->SetFont(
'',
'', 9);
109 $pdf->SetTextColor(100, 100, 100);
112 $pdf->SetTextColor(0, 0, 0);
115 h3 { color: #333; border-bottom: 1px solid #ccc; font-size: 12pt; margin-top: 15px; }
116 table { width: 100%; border-collapse: collapse; }
117 th { background-color: #f0f0f0; font-weight: bold; padding: 5px; border: 1px solid #ccc; }
118 td { padding: 5px; border: 1px solid #ccc; }
119 .key { font-weight: bold; width: 30%; background-color: #f9f9f9; }
124 if (isset($data[
'customer']) && isset($data[
'details'])) {
126 $html .=
'<h3>' . $langs->trans(
'ThirdpartyDetails') .
'</h3>';
127 $html .=
'<table cellpadding="4">';
128 foreach ($data[
'customer'] as $k => $v) {
130 if ($k ===
'url' || $k ===
'id' || $k ===
'rowid') {
138 foreach ($data[
'details'] as $section => $rows) {
139 $sectionTitle =
dol_escape_htmltag(ucwords((
string) str_replace(
'_',
' ', $section)));
140 $html .=
'<h3>' . $sectionTitle .
'</h3>';
142 if (empty($rows) || !is_array($rows)) {
143 $html .=
'<p>' . $langs->trans(
'NoDataAvailable') .
'</p>';
146 $keys = array_keys($rows[0]);
147 $keys = array_filter(
152 static function (
string $k) {
153 return $k !==
'url' && $k !==
'rowid';
157 $html .=
'<table cellpadding="4"><thead><tr>';
158 foreach ($keys as $k) {
161 $html .=
'</tr></thead><tbody>';
162 foreach ($rows as $row) {
164 foreach ($keys as $k) {
165 $val = isset($row[$k]) ? $row[$k] :
'-';
166 if (is_array($val)) {
170 if (is_string($val) && strpos($val,
'<a href') !==
false) {
171 $html .=
'<td>' . $val .
'</td>';
178 $html .=
'</tbody></table>';
181 } elseif (isset($data[0]) && is_array($data[0])) {
183 $keys = array_keys($data[0]);
184 $keys = array_filter(
189 static function (
string $k) {
190 return $k !==
'url' && $k !==
'rowid';
194 $html .=
'<table cellpadding="4"><thead><tr nobr="true">';
195 foreach ($keys as $key) {
196 $html .=
'<th>' .
dol_escape_htmltag(strtoupper(str_replace(
'_',
' ', $key))) .
'</th>';
198 $html .=
'</tr></thead><tbody>';
199 foreach ($data as $row) {
200 $html .=
'<tr nobr="true">';
201 foreach ($keys as $key) {
202 $val = isset($row[$key]) ? $row[$key] :
'';
203 if (is_array($val)) {
204 $val = count($val) .
' items';
206 if (is_string($val) && strpos($val,
'<a href') !==
false) {
207 $html .=
'<td>' . $val .
'</td>';
214 $html .=
'</tbody></table>';
217 $html .=
'<table cellpadding="5">';
218 foreach ($data as $key => $val) {
220 if ($key ===
'url') {
223 $valStr = is_array($val) ? json_encode($val) : $val;
229 $pdf->writeHTML($html,
true,
false,
true,
false,
'');
230 $pdf->Output($filename,
'D');
232 dol_syslog(
"Error generating PDF: " . $e->getMessage(), LOG_ERR);
233 print
"PDF Error: Unable to generate PDF. Please contact administrator.";
dol_now($mode='gmt')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
pdf_getFormat($outputlangs=null, $mode='setup')
Return array with format properties of default PDF format.
pdf_getPDFFontSize($outputlangs)
Return font size to use for PDF generation.
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.