How can I combine text resizing with bolding in footer? - php

I can alternate bold and normal text in a footer (thanks to ejuhjav) but only if I don't try to style the text.
In the following example, the letter 'T' comes out bold because it's at the default size (12).
If I reduce the text, the bolding is dropped. I've also tried using named font styles and created font style objects. No luck.
Is there something simple I'm missing?
// create footer
$footer = $section->addFooter();
$textrun = $footer->addTextRun();
// define bold style
$boldFontStyleName = 'BoldText';
$phpWord->addFontStyle($boldFontStyleName, array('bold' => true));
// add content
$textrun->addText('T', $boldFontStyleName);
$textrun->addText(' ++353 1 555 0001 ',
array('name' => 'Helvetica', 'size' => 8));
$textrun->addText('E',
array('name' => 'Helvetica', 'size' => 8), $boldFontStyleName);
$textrun->addText(' abc.def#ghk.ie ',
array('name' => 'Helvetica', 'size' => 8));
$textrun->addText('W',
array('name' => 'Helvetica', 'size' => 8), $boldFontStyleName);
$textrun->addText(' abcd.ie/wxz',
array('name' => 'Helvetica', 'size' => 8));

and here goes the third time then :)
the addText function definition is:
addText(string $text, mixed $fStyle = null, mixed $pStyle = null)
i.e. the font styles are given with the second parameter and thus the rows where you have the $boldFontStyleName variable as third attribute the font is not bolded.
The easiest way to fix this would be to to just define couple of additional font styles:
// create footer
$footer = $section->addFooter();
$textrun = $footer->addTextRun();
// define font styles
$boldFontStyleName = 'BoldText';
$phpWord->addFontStyle($boldFontStyleName, array('bold' => true));
$smallFontStyleName = 'smallText';
$phpWord->addFontStyle($smallFontStyleName, array(
'name' => 'Helvetica',
'size' => 8,
));
$boldSmallFontStyleName = 'BoldSmallText';
$phpWord->addFontStyle($boldSmallFontStyleName, array(
'bold' => true,
'name' => 'Helvetica',
'size' => 8,
));
// add content
$textrun->addText('T', $boldFontStyleName);
$textrun->addText(' ++353 1 555 0001 ', $smallFontStyleName);
$textrun->addText('E', $boldSmallFontStyleName);
$textrun->addText(' abc.def#ghk.ie ', $smallFontStyleName);
$textrun->addText('W', $boldSmallFontStyleName);
$textrun->addText(' abcd.ie/wxz', $smallFontStyleName);

Related

PHPWord - setting font inside Template Processor

I am using PHPWord to load a template file and create a new one from it.
$templateName = 'QuoteTemplate1.docx';
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($templateName);
$values = ['clientAddressName' => $quote->company_name]; // this array typically has more values
foreach ($values as $key => $value) {
$templateProcessor->setValue($key, $value);
}
Then I am adding a custom built table to this template, the code is like this:
$table = new PhpOffice\PhpWord\Element\Table([
'borderSize' => 0,
'borderColor' => 'none',
'width' => 9200,
'unit' => PhpOffice\PhpWord\SimpleType\TblWidth::TWIP
]);
$table->addRow();
$table->addCell(150)->addText('Cell A1');
$table->addCell(150)->addText('Cell A2');
$table->addCell(150)->addText('Cell A3');
$table->addRow();
$table->addCell(150)->addText('Cell B1');
$table->addCell(150)->addText('Cell B2');
$table->addCell(150)->addText('Cell B3');
$templateProcessor->setComplexBlock('quoteItemTable', $table);
I want to add font and paragraph styles to the text in this custom table - and here lies the problem.
If I try something like this:
$templateProcessor->addParagraphStyle('rightAlign', ['alignment' => 'right']);
The I get errors (addParagraphStyle is not a recognized method of $templateProcessor). And if I try:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addParagraphStyle('rightAlign', ['alignment' => 'right']);
$table->addCell(25)->addText('Cell A1', 'fontStyle', 'rightAlign');
Then I get no errors, but my rightAlign paragraph style is ignored. Please note I get the same results when I try the steps with font styles as well as paragraph styles.
How can I set my own font and paragraph styles inside a template processor?
The following worked for me:
$my_template = new \PhpOffice\PhpWord\TemplateProcessor(storage_path('template1.docx'));
$table = new \PhpOffice\PhpWord\Element\Table();
$myFontStyle = array('name' => 'Minion Pro', 'size' => 10, 'bold' => true);
$myParagraphStyle = array('align'=>'center', 'spaceBefore'=>50, 'spaceafter' => 50);
$table->addRow();
$table->addCell()->addText('Cell 1', $myFontStyle, $myParagraphStyle );
$table->addCell()->addText('Cell 2', $myFontStyle, $myParagraphStyle );
$table->addCell()->addText('Cell 3', $myFontStyle, $myParagraphStyle );
$my_template->setComplexBlock('table', $table);

Set left side width of mPDF header

I am using mPDF for exporting contents as a PDF file. For creating header, I am using $mpdf->SetHeader() function like this:
$arr = array (
'L' => array (
'content' => 'Bozmadan sonra ıslah yapılamaz kuralının istisnası, ...',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif'
),
'R' => array (
'content' => '{PAGENO}',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'line' => 1
);
$mpdf->SetHeader($arr, 'O');
This gives us like this:
You see, I don't want center area but it creates. How can I code for more wider left side, narrower right side and no center area.
If you use SetHTMLHeader() instead of SetHeader(), you can style it in HTML and have more control over the layout.
$header= '<table width="100%"><tr>
<td width="80%">Bozmadan sonra ıslah yapılamaz kuralının istisnası, ...</td>
<td width="20%">{PAGENO}</td>
</tr></table>';
$mpdf->SetHTMLHeader($header);

$textrun->addText content being processed before $footer->addText content

I want to have a footer that contains 3 lines of text, with a vertical space (like a blank line) between lines 2 and 3.
Because line 3 contains bold and normal text, I have to implement it as a textrun.
But there should be a line break between lines 1 and 2, so I use addText for both of these.
Unfortunately, the order in which the footer content is displayed is as follows:
textrun
footerText1
footerText2
The textrun gets processed first and appears above the other lines!
How do I get the order right?
My footer code is:
// create footer
$footer = $section->addFooter();
// textrun declaration removed from here
// create footer content
$footerText1 = "Blah blah blah.";
$footerText2 = "Ipsum loret Ipsum loret Ipsum loret.";
// define font styles
$smallFontStyleName = 'smallText';
$phpWord->addFontStyle($smallFontStyleName, array(
'name' => 'Helvetica',
'size' => 8,
));
$boldSmallFontStyleName = 'BoldSmallText';
$phpWord->addFontStyle($boldSmallFontStyleName, array(
'bold' => true,
'name' => 'Helvetica',
'size' => 8,
));
// define paragraph spacing styles
$phpWord->addParagraphStyle('line1FooterStyle', array( 'spaceAfter'=>20));
$phpWord->addParagraphStyle('line2FooterStyle', array( 'spaceAfter'=>380));
// add content
$footer->addText($footerText1,
array('name' => 'Helvetica', 'size' => 8),
array('space' => array('after' => 20))
);
$footer->addText($footerText2,
array('name' => 'Helvetica', 'size' => 8),
array('space' => array('after' => 380))
);
// textrun relocated to here
$textrun = $footer->addTextRun();
$textrun->addText('T', $boldSmallFontStyleName);
$textrun->addText(' ++353 1 555 0001 ', $smallFontStyleName);
$textrun->addText('E', $boldSmallFontStyleName);
$textrun->addText(' abc.def#ghk.ie ', $smallFontStyleName);
$textrun->addText('W', $boldSmallFontStyleName);
$textrun->addText(' abcd.ie/wxz', $smallFontStyleName);
OK, I saw the problem and fixed it. I had declared the textrun before the $footer->addText lines. Which means the textrun code was inserted first, incorrectly. D'oh!

mPDF - Header for First Page only but Footer for every page

I need to set the header for FIRST page only and the footer for every page. Can't figure a way and I've been search for the solution DAYS already. Currently my code goes like this
$mpdf=new mPDF();
$mpdf->useOddEven = true;
$mpdf->SetHTMLHeader('<div style="text-align: right;"><img src="var:images"
width="80px"/></div>', 'O');
$mpdf->SetHTMLFooter('<div style="text-align: left; font-family: Arial, Helvetica,
sans-serif; font-weight: bold;font-size: 7pt; ">Footer</div>');
$html = 'Content';
$mpdf->WriteHTML($html);
$mpdf->Output();
I've set the header to Odd however, both header and footer appears on other odd page (3, 5, 7,... page). Is there a way to make the header first page only but the footer appears in every page?
Currently using MPDF5.6
According to the Documentation:
https://mpdf.github.io/reference/mpdf-functions/sethtmlheader.html
The Third Parameter is:
write
If TRUE it forces the Header to be written immediately to the current page. Use if the header is being set after the new page has been added.
DEFAULT: FALSE
SO..
$mpdf->SetHTMLHeader('<div style="text-align: right;"><img src="var:images" width="80px"/></div>', 'O', true);
$mpdf->SetHTMLHeader('');
$footer = array (
'L' => array (
'content' => 'Footer',
'font-size' => 7,
'font-style' => 'B',
'font-family' => 'Arial, Helvetica, sans-serif',
'color'=>'#000000'
),
'C' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'R' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'line' => 1,
);
$mpdf->SetFooter($footer);
Only the SetFooter Method can set for the footer for both ODD & EVEN pages.

Position h1,h2,h3 and other tags with TCPDF

I am trying to make a PDF document with TCPDF using HTML code.
At the moment I use this code:
// set font
$pdf->SetFont('dejavusans', '', 36);
// add a page
$pdf->AddPage();
$html = '
<style>
.h1 {
color: #2B6999;
font-weight: normal;
}
</style>
<h1 class="h1">Test</h1>
';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, 'C');
How can I position this text? I cannot use between the tags margin-top etc..
Can anyone help me with this problem?
You can add something like this:
$tagvs = array('h1' => array(0 => array('h' => 1, 'n' => 3), 1 => array('h' => 1, 'n' => 2)),
'h2' => array(0 => array('h' => 1, 'n' => 2), 1 => array('h' => 1, 'n' => 1)));
$pdf->setHtmlVSpace($tagvs);
And here is the format description from the docs / examples:
File: tcppdf.php :
/**
* Set the vertical spaces for HTML tags.
* The array must have the following structure (example):
* $tagvs = array('h1' => array(0 => array('h' => '', 'n' => 2), 1 => array('h' => 1.3, 'n' => 1)));
* The first array level contains the tag names,
* the second level contains 0 for opening tags or 1 for closing tags,
* the third level contains the vertical space unit (h) and the number spaces to add (n).
* If the h parameter is not specified, default values are used.
* #param $tagvs (array) array of tags and relative vertical spaces.
* #public
* #since 4.2.001 (2008-10-30)
*/
File http://www.tcpdf.org/examples/example_061.phps :
// REMOVE TAG TOP AND BOTTOM MARGINS
//
// $tagvs = array('p' => array(0 => array('h' => 0, 'n' => 0), 1 => array('h' => 0, 'n' => 0)));
// $pdf->setHtmlVSpace($tagvs);
//
// Since the CSS margin command is not yet implemented on TCPDF, you
// need to set the spacing of block tags using the above method.
You are using writeHTML which outupts the HTML exactly, you need to use the "$pdf->Cell" function. There are lots of example on this here http://www.tcpdf.org/examples.php

Categories