AnsweredAssumed Answered

How do I save a PDF locally on the server?

Question asked by Kenneth Brill on Jul 5, 2019
Latest reply on Jul 5, 2019 by Francesca Shiekh

I have tried about a hundred times to get this tight but at best I am left with a mangled PDF (logo issues mostly) . I just want to take a PDF Manager record and turn it into a PDF and save that PDF locally on the server.  I can download the PDF Manager record from the module and it looks fine and I just what that file on the Servers HDD.

 

I tried this code (edited - I might have missed something but this is the jist of it)

<?php
$template = $this->get_PDF_html($pdf_template);
          $base_module = $template['type'];

          if ($record_id != '') {
               $bean = BeanFactory::retrieveBean($base_module, $record_id);
          }

          $object_arr = array();
          $object_arr[$base_module] = $bean->id;

          if ($base_module === 'Contacts') {
               $object_arr['Accounts'] = $bean->account_id;
          }

          $search = array(
               '@<script[^>]*?>
.*?</script>@si',// Strip out javascript
               '@<[\/\!]*?[^<>]*?>@si',        // Strip out HTML tags
               '@([\r\n])[\s]+@',            // Strip out white space
               '@&(quot|#34);@i',            // Replace HTML entities
               '@&(amp|#38);@i',
               '@&(lt|#60);@i',
               '@&(gt|#62);@i',
               '@&(nbsp|#160);@i',
               '@&(iexcl|#161);@i',
               '@<address[^>]*?>@si'
          );

          $replace = array(
               '',
               '',
               '\1',
               '"',
               '&',
               '<',
               '>',
               ' ',
               chr(161),
               '<br>'
          );

          $text = preg_replace($search, $replace, $template['html']);
          $pdf_html_parser = new templateParser();
          $parse_html = $pdf_html_parser->parse_template($text, $object_arr);
          $file = $this->create_PDF($parse_html, $record_module);
//MORE CODE AFTER THIS BUT NOT RELATED

     public function get_PDF_html($pdf_id)
     {
          $html = '';
          $page_break = '<div style="page-break-before:always">&nbsp;</div>';
          $is_first_page = true;
          $template = array();
          $pdf_template = new PdfManager();
          $pdf_template->retrieve($pdf_id);
          $template['id'] = $pdf_template->id;
          $template['name'] = $pdf_template->name;
          $template['type'] = $pdf_template->base_module;
          $template['html'] = $pdf_template->body_html;
          return $template;
     }

     public function create_PDF($html, $name)
     {
          require_once('vendor/tcpdf/tcpdf.php');
          $tcpdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
          $tcpdf->SetCreator(PDF_CREATOR);
          $name = str_replace(' ', '_', $name);
          $date = date('Y-m-d');
          $file_info['file_path'] = "upload/" . $name . '_' . $date . ".pdf";
          $file_info['file_name'] = $name . '_' . $date . ".pdf";
          $tcpdf->AddPage();
          $tcpdf->writeHTML($html, true, false, true, false, '');
          $tcpdf->Output($file_info['file_path'], "F");
          return $file_info;
     }

 

This code gives me a PDF but it does not look like the PDF I download from the app menu.  Dos anyone have a clue how I would do this?

Outcomes