0% found this document useful (0 votes)
4 views2 pages

Todo Letter Report Bugs

Uploaded by

adarsha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Todo Letter Report Bugs

Uploaded by

adarsha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

public function exportLetter($client_id = null, $id = null)

{
$this->load->library('user_agent');
$letterRow = $this->letter_model->getClientLetters(urlDecrypt($client_id),
$id);

if (!$letterRow) {
$this->session->set_flashdata('err_msg', 'Unable to export the letter.
Please try again.');
redirect($this->agent->referrer());
return;
}

$htmlContent = $letterRow->message;

$phpWord = new \PhpOffice\PhpWord\PhpWord();


$section = $phpWord->addSection();

$dom = new \DOMDocument();


@$dom->loadHTML($htmlContent);
foreach ($dom->getElementsByTagName('body')->item(0)->childNodes as $node)
{
if ($node->nodeName === 'table') {
$this->processTable($node, $section);
} else {
$htmlFragment = $dom->saveHTML($node);
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $htmlFragment,
false, false);
}
}

$result = $this->letter_model->saveLetterFromS3($letterRow);
if ($result && isset($result['path'])) {
$phpWord->save($result['path'], 'Word2007');
header('Content-Type: application/vnd.openxmlformats-
officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="' .
$result['fileName'] . '"');
header('Content-Length: ' . filesize($result['path']));

ob_clean();
flush();
readfile($result['path']);
unlink($result['path']);
exit;
} else {
$this->session->set_flashdata('err_msg', 'Unable to save the letter to
S3. Please try again.');
redirect($this->agent->referrer());
}
}

// function to process table nodes and add them to the section


private function processTable($tableNode, $section)
{
$table = $section->addTable(['borderSize' => 6, 'borderColor' =>
'000000']);
foreach ($tableNode->getElementsByTagName('tr') as $tr) {
$tableRow = $table->addRow();
foreach ($tr->getElementsByTagName('td') as $td) {
$cellText = trim($td->nodeValue);
$tableRow->addCell(2000, ['borderSize' => 6, 'borderColor' =>
'000000'])
->addText($cellText);
}
}
}

exportLetter()

You might also like