Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
33f1619
[BUGFIX] allow to load unclean html without exception
kaystrobach Mar 29, 2017
b54200e
Merge branch 'develop' into patch-1
troosan Dec 3, 2018
6e00210
stop building for 5.3, 5.4, 5.5 and 7.0
troosan Dec 30, 2018
2968370
update dependencies to minimum php 5.6 dependencies
troosan Dec 31, 2018
2a8a32e
add alias for 1.0-dev branch
troosan Jan 2, 2019
5bc9250
Fix unit tests
troosan Jan 2, 2019
06a9271
remove whitespaces
troosan Jan 2, 2019
8d92409
Merge pull request #1032 from kaystrobach/patch-1
troosan Jan 2, 2019
b54f391
Merge pull request #1545 from troosan/stop_building_for_php5.3
troosan Jan 2, 2019
847520f
Merge branch 'master' into develop_v1.0
troosan Oct 1, 2019
9914f3d
Merge branch 'develop' into develop_v1.0
Dec 8, 2019
590a8b7
build with php nightly
Dec 9, 2019
711fcde
still allow php 7.4 to fail because of dompdf
Dec 9, 2019
bf52f2d
php 7.4 not yet available
Dec 9, 2019
6d50133
update 7.4 in travis build
Dec 14, 2019
087628d
update to phpunit 7.5
Dec 14, 2019
3b8d1cf
Add TemplateProcessor.setChart
dbarzin May 7, 2020
90fef2c
Add TemplateProcessor.setChart
dbarzin May 7, 2020
52b2175
small bugs in element chart
dbarzin May 7, 2020
ee83fed
Revert "small bugs in element chart"
dbarzin May 7, 2020
3386548
Fix color change with multiple series
dbarzin May 28, 2020
f98db98
Revert "Fix color change with multiple series"
dbarzin May 28, 2020
c79653c
Change color assignment
dbarzin Jun 6, 2020
75ef6bf
Revert "Change color assignment"
dbarzin Jun 8, 2020
e56a39a
fix bug in Word
dbarzin Jun 8, 2020
67652f3
fix color bug in Charts
dbarzin Jun 8, 2020
6700fc4
color rotate in Charts
dbarzin Jun 8, 2020
762b1c7
Replace tabs by spaces
dbarzin Jun 18, 2020
5b830c2
Merge branch 'develop' of https://github.com/troosan/PHPWord into tmp
Feb 5, 2021
77de606
Merge branch 'develop' into develop
troosan Feb 5, 2021
79c5552
undo changes not relevant to PR
Feb 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# git files
/.gitignore export-ignore
/.gitattributes export-ignore
/.github export-ignore

# project directories
/build export-ignore
Expand Down
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ script:
- if [ -z "$COVERAGE" ]; then ./vendor/bin/phpmd src/,tests/ text ./phpmd.xml.dist --exclude pclzip.lib.php ; fi
## PHPUnit
- ./vendor/bin/phpunit -c ./ $(if [ -n "$COVERAGE" ]; then echo --coverage-text; else echo --no-coverage; fi)
## PHPLOC
- if [ -z "$COVERAGE" ]; then ./vendor/bin/phploc src/ ; fi
## PHPDocumentor
##- if [ -z "$COVERAGE" ]; then ./vendor/bin/phpdoc -q -d ./src -t ./build/docs --ignore "*/src/PhpWord/Shared/*/*" --template="responsive-twig" ; fi

Expand Down
10 changes: 10 additions & 0 deletions docs/templates-processing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ You can also set multiple values by passing all of them in an array.

$templateProcessor->setValues(array('firstname' => 'John', 'lastname' => 'Doe'));

setChartValue
"""""""""""""
Replace a variable by a chart.

.. code-block:: php
$categories = array('A', 'B', 'C', 'D', 'E');
$series1 = array(1, 3, 2, 5, 4);
$chart = new Chart('doughnut', $categories, $series1);
$templateProcessor->setChartValue('myChart', $chart);

setImageValue
"""""""""""""
The search-pattern model for images can be like:
Expand Down
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
colors="true"
Expand Down
45 changes: 45 additions & 0 deletions samples/Sample_41_TemplateSetChart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
use PhpOffice\PhpWord\Element\Chart;
use PhpOffice\PhpWord\Shared\Converter;

include_once 'Sample_Header.php';

// Template processor instance creation
echo date('H:i:s'), ' Creating new TemplateProcessor instance...', EOL;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('resources/Sample_41_TemplateSetChart.docx');

$chartTypes = array('pie', 'doughnut', 'bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column');
$twoSeries = array('bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column');
$threeSeries = array('bar', 'line');

$categories = array('A', 'B', 'C', 'D', 'E');
$series1 = array(1, 3, 2, 5, 4);
$series2 = array(3, 1, 7, 2, 6);
$series3 = array(8, 3, 2, 5, 4);

$i=0;
foreach ($chartTypes as $chartType) {
$chart = new Chart($chartType, $categories, $series1);

if (in_array($chartType, $twoSeries)) {
$chart->addSeries($categories, $series2);
}
if (in_array($chartType, $threeSeries)) {
$chart->addSeries($categories, $series3);
}

$chart->getStyle()
->setWidth(Converter::inchToEmu(3))
->setHeight(Converter::inchToEmu(3));

$templateProcessor->setChart("chart{$i}", $chart);
$i++;
}

echo date('H:i:s'), ' Saving the result document...', EOL;
$templateProcessor->saveAs('results/Sample_41_TemplateSetChart.docx');

echo getEndingNotes(array('Word2007' => 'docx'), 'results/Sample_41_TemplateSetChart.docx');
if (!CLI) {
include_once 'Sample_Footer.php';
}
Binary file added samples/resources/Sample_41_TemplateSetChart.docx
Binary file not shown.
3 changes: 3 additions & 0 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public static function addHtml($element, $html, $fullHTML = false, $preserveWhit
$html = '<body>' . $html . '</body>';
}

//need to remove whitespaces between tags, as loadHTML seems to take those into account
$html = preg_replace('/(\>)\s*(\<)/m', '$1$2', $html);

// Load DOM
if (\PHP_VERSION_ID < 80000) {
$orignalLibEntityLoader = libxml_disable_entity_loader(true);
Expand Down
41 changes: 41 additions & 0 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,47 @@ public function setValues(array $values)
}
}


/**
* @param string $search
* @param \PhpOffice\PhpWord\Element\AbstractElement $complexType
*/
public function setChart($search, \PhpOffice\PhpWord\Element\AbstractElement $chart)
{
$elementName = substr(get_class($chart), strrpos(get_class($chart), '\\') + 1);
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName;

// Get the next relation id
$rId= $this->getNextRelationsIndex($this->getMainPartName());
$chart->setRelationId($rId);

// Define the chart filename
$filename = "charts/chart{$rId}.xml";

// Get the part writer
$writerPart = new \PhpOffice\PhpWord\Writer\Word2007\Part\Chart();
$writerPart->setElement($chart);

// ContentTypes.xml
$this->zipClass->addFromString("word/{$filename}", $writerPart->write());

// add chart to content type
$xmlRelationsType = "<Override PartName=\"/word/{$filename}\" ContentType=\"application/vnd.openxmlformats-officedocument.drawingml.chart+xml\"/>";
$this->tempDocumentContentTypes = str_replace('</Types>', $xmlRelationsType, $this->tempDocumentContentTypes) . '</Types>';

// Add the chart to relations
$xmlChartRelation = "<Relationship Id=\"rId{$rId}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart\" Target=\"charts/chart{$rId}.xml\"/>";
$this->tempDocumentRelations[$this->getMainPartName()] = str_replace('</Relationships>', $xmlChartRelation, $this->tempDocumentRelations[$this->getMainPartName()]) . '</Relationships>';

// Write the chart
$xmlWriter = new XMLWriter();
$elementWriter = new $objectClass($xmlWriter, $chart, true);
$elementWriter->write();

// Place it in the template
$this->replaceXmlBlock($search, "<w:p>" . $xmlWriter->getData() . "</w:p>", 'w:p');
}

private function getImageArgs($varNameWithArgs)
{
$varElements = explode(':', $varNameWithArgs);
Expand Down
22 changes: 11 additions & 11 deletions src/PhpWord/Writer/Word2007/Part/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ private function writeSeries(XMLWriter $xmlWriter, $scatter = false)
$colors = $style->getColors();

$index = 0;
$colorIndex = 0;
foreach ($series as $seriesItem) {
$categories = $seriesItem['categories'];
$values = $seriesItem['values'];
Expand Down Expand Up @@ -265,23 +266,21 @@ private function writeSeries(XMLWriter $xmlWriter, $scatter = false)
$this->writeSeriesItem($xmlWriter, 'cat', $categories);
$this->writeSeriesItem($xmlWriter, 'val', $values);

// setting the chart colors was taken from https://github.com/PHPOffice/PHPWord/issues/494
if (is_array($colors) && count($colors)) {
// This is a workaround to make each series in a stack chart use a different color
if ($this->options['type'] == 'bar' && stristr($this->options['grouping'], 'stacked')) {
array_shift($colors);
}
$colorIndex = 0;
foreach ($colors as $color) {
// check that there are colors
if (is_array($colors) && count($colors)>0) {
// assign a color to each value
$valueIndex=0;
foreach ($values as $value) {
// check that there are still enought colors
$xmlWriter->startElement('c:dPt');
$xmlWriter->writeElementBlock('c:idx', 'val', $colorIndex);
$xmlWriter->writeElementBlock('c:idx', 'val', $valueIndex);
$xmlWriter->startElement('c:spPr');
$xmlWriter->startElement('a:solidFill');
$xmlWriter->writeElementBlock('a:srgbClr', 'val', $color);
$xmlWriter->writeElementBlock('a:srgbClr', 'val', $colors[$colorIndex++ % count($colors)]);
$xmlWriter->endElement(); // a:solidFill
$xmlWriter->endElement(); // c:spPr
$xmlWriter->endElement(); // c:dPt
$colorIndex++;
$valueIndex++;
}
}
}
Expand Down Expand Up @@ -414,6 +413,7 @@ private function writeShape(XMLWriter $xmlWriter, $line = false)
}
$xmlWriter->endElement(); // a:ln
$xmlWriter->endElement(); // c:spPr

}

private function writeAxisTitle(XMLWriter $xmlWriter, $title)
Expand Down