I am using a table with addHtml method to export HTML to docx file in phpWord. But the font of text is Times New Roman (font-size 12), meanwhile, the font should be Arial (font-size 10) by default. I did not set anything in code. I also tried something but it seems not to work. Here is my code:
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('modules/admin/views/customers/resume_tpl.docx');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontName('Arial');
$phpWord->setDefaultFontSize(10);
$section = $phpWord->addSection();
$wordTable = $section->addTable();
$wordTable->addRow();
$cell = $wordTable->addCell();
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, str_replace("&","&", $model[self_assessment])); // $model[self_assessment] is the HTML text
// $templateProcessor->setDefaultFontName('Arial');
// $templateProcessor->setDefaultFontSize(10);
$templateProcessor->setComplexBlock('assess', $wordTable);
All I want is to set font to 'Arial' and font-size 10.
I tested with the addText() method with your code and it does change the font-name and font-size of the table, by reference post here Formatting a text in a table cell with PHPWord e.g. bold, font, size e.t.c
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('test.docx');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$wordTable = $section->addTable();
$wordTable->addRow();
$wordTable
->addCell()
->addText(
str_replace("&","&", "HTML Texting"),
array(
'name' => 'Arial',
'size' => 10,
);
);
$templateProcessor->setComplexBlock('assess', $wordTable);
$templateProcessor->saveAs('result.docx');
Hope it helps!
And I think this sample might help out for you too
https://github.com/PHPOffice/PHPWord/blob/master/samples/Sample_40_TemplateSetComplexValue.php
I kind of figured it out, although I don't like this way :v. Because my text is from CK Editor, so it has HTML format. I just add css style to HTML and it works. Something like this:
$model[self_assessment] = str_replace("&","&", $model[self_assessment]);
$assess = str_replace("<p>", "<p style='font-size: 10pt; font-family: Arial;'>", $model[self_assessment]);
$assess = str_replace("<ul>", "<ul style='font-size: 10pt; font-family: Arial;'>", $assess);
$assess = str_replace("<ol>", "<ol style='font-size: 10pt; font-family: Arial;'>", $assess);
Thank you all.
Related
I'm attempting to render barcodes for a MVC application in laminas.
I can build the barcode object, and the renderer, but when I render it it blacks out the whole screen with a single white square in the middle.
I tried replacing my code with the following from PHP Snippets, and I got the same issue. Am I missing a php extension or a composer package or something?
<?php use Common\Util;
use Laminas\Barcode\Object\Code128;
use Laminas\Barcode\Renderer\Image;
$barcode = new Code128([
'text' => 'PHP Snippets',
'barHeight' => 60,
'factor' => 2,
]);
$renderer = new Image([
'resource' => imagecreate($barcode->getWidth(), $barcode->getHeight()),
'barcode' => $barcode,
]);
ob_start();
$renderer->render();
$image = base64_encode(ob_get_contents());
ob_end_clean();
echo '<img src="data:image/png;base64,' . $image . '">';
?>
Here is the html that renders. For some reason it renders the whole <body> as a single image, from the bar code.
<body style="margin: 0px; background: #0e0e0e; height: 100%"><img style="-webkit-user-select: none;margin: auto;background-color: hsl(0, 0%, 90%);transition: background-color 300ms;" src="http://mymvcapp.com/orders"></body>
And here is what displays over the whole browser tab.
I've altered the default code that is generated when you upload an image through the posting page with the "add media" button in the post content.
This is the code I am using:
add_filter('image_send_to_editor', 'img_wrapper', 20, 8);
function img_wrapper($html, $id, $caption, $title, $align, $url, $size, $alt){
$class = "IA_image";
$editor_size = 300;
$img_meta = wp_get_attachment_metadata($id);
$width = $img_meta['width'];
$height = $img_meta['height'];
$url = wp_get_original_image_url($id);
$style = "width: {$editor_size}px; height: {$editor_size}px; background-image: url({$url}); background-position: center; background-size: cover;";
$html = "<div class=\"{$class}\" data-width=\"{$width}\" data-height=\"{$height}\" style=\"{$style}\"></div>";
// Add caption
if($caption){
$style = "font-style: italic;";
$html .= "<p class=\"caption\" style=\"{$style}\">{$caption}</p>";
}
$html = "<div class=\"{$class}-outer\">{$html}</div>";
return $html;
}
Instead of inserting an img tag I'm now using divs and setting the background image on them accordingly. I'm displaying these images on the wordpress single page.
However, by doing this, the image can not be edited like the usual images (an edit button appears when you click on it). This is the edit button I'm referring to:
Is there any way I can add this "edit" button to my altered images?
I am building a system where the requirement says no links to CSS are allowed. They do allow to place all CSS content within the style element.
I am using DOMDocument to build the XML/XHTML.
The CSS stylesheets are around 320 lines so I would pefer to construct them in separate CSS files and solve the insertion of the CSS content in the DomDocument build.
Question:
What is the best way of inserting an external CSS file content
and get it in place between the DOMDocument built style element?
Index.php
<?php
$xml = new DomDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$html = $xml->createElement('html');
$xml->appendChild($html);
$head = $xml->createElement('head');
$html->appendChild($head);
//
$style = $xml->createElement(
'style',
'css-content....' // The CSS content from external file should be inserted here.
);
$style->setAttribute('type', 'text/css');
$head->appendChild($style);
echo $xml->saveXML();
Main.css
body {
background-color: pink;
}
Wanted result
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<style type="text/css">
body {
background-color: pink;
}
</style>
</head>
</html>
Try something along these lines:
Add
$css = file_get_contents('main.css');
and change $style to:
$style = $xml->createElement('style', $css);
and it should work.
I am converting an html file to PDF by using mPdf, but the returned PDF file includes blank pages. I want to delete those blank pages.
Had the same problem and realized that my CSS was forcing these page breaks. Make sure you don't have this in your CSS:
page-break-after: always;
It may be for many reasons:
1) Make sure the elements doesn't have unnecessary margins nor Paddings
2) Configure correctly the page properties (specially margins) :
$page_orientation = 0;
$page_size = 'Letter';
$page_margins = array('LEFT(int)','RIGHT(int)','UP(int)','BOTTOM(int)');
$pdf_output = 'I';
$css_files = array(
'path/file.css',
'path/file_2.css',
);
$orientationPage = array('','-L');
/* ===== [ MPDF ] =====*/
$mpdf=new mPDF('utf-8', $page_size.$orientationPage[$page_orientation],'','',$page_margins[0],$page_margins[1],$page_margins[2],$page_margins[3]);
// Loading CSS
for($i = 0; $i < count($css_files); $i++){
$stylesheet = file_get_contents('../../'.$css_files[$i]);
$mpdf->WriteHTML($stylesheet,1); // 1 para indicar que es CSS
}
// Set header & Footer (This are variables with html code)
$mpdf->SetHTMLHeader($header);
$mpdf->SetHTMLFooter($footer);
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetTitle($title);
$mpdf->WriteHTML($html); // <-- insert HTML
// Create PDF
$mpdf->Output($titulo.'.pdf',$pdf_output);
3) Make sure you haven't unnecessary "Page Breaks" on the HTML
<pagebreak type="NEXT-ODD" resetpagenum="1" pagenumstyle="i" suppress="off" />
I hope it can help you!
I had the same problem and i fixed it by removing the AddPage property from my code
I changed the following code
// Code with bug
$mpdf = new mPDF('utf-8', array(380,500));
$mpdf->WriteHTML($htmlContent);
$mpdf->AddPage('P'); // It will add extra page - I that i removed this line
$mpdf->Output();
Into this
// code after resolving the bug
$mpdf = new mPDF('utf-8', array(380,500));
$mpdf->WriteHTML($htmlContent);
$mpdf->Output();
I'm looking to write a script in php that scans an html document and adds new markup to a element based on what it finds. More specifically, I was it to scan the document and for every element it searches for the CSS markup "float: right/left" and if it locates it, it adds align="right/left" (based on what it finds).
Example:
<img alt="steve" src="../this/that" style="height: 12px; width: 14px; float: right"/>
becomes
<img alt="steve" src="../this/that" align="right" style="height: 12px; width: 14px; float: right"/>
$dom = new DOMDocument();
$dom->loadHTML($htmlstring);
$x = new DOMXPath($dom);
foreach($x->query("//img[contains(#style,'float: right']") as $node) $node->setAttribute('align','right');
foreach($x->query("//img[contains(#style,'float: left']") as $node) $node->setAttribute('align','left');
edit:
When there is no certainty of amount of space between 'float:' & 'right', there are several options:
Use the XPath 1.0: //img[starts-with(normalize-space(substring-after(#style,'float:')),'right')]
Just do a simple check for float like //img[contains(#style,'float:'], and check with $node->getAttribute() what actually comes afterwards.
Import preg_match into the equasion (which was just recently pointed out to me (thanks Gordon), but in this case is imho the least favorite solution):
.
$dom = new DOMDocument();
$dom->loadHTML($htmlstring);
$x = new DOMXPath($dom);
$x->registerNamespace("php", "http://php.net/xpath");
$x->registerPHPFunctions('preg_match');
foreach($x->query("//img[php:functionString('preg_match','/float\s*:\s*right/',#style)]") as $node) $node->setAttribute('align','right');
Please please, don't use a regexp to parse HTML.
Use simple_html_dom instead.
$dom = new simple_html_dom();
$dom->load($html);
foreach ($dom->find("[style=float: left],[style=float: right]") as $fragment)
{
if ($fragment[0]->style == 'float:left')
{
$fragment[0]->align='left';
$fragment[0]->style = '';
}
...
}
echo $dom;