banner



How To Make Templates Where You Can Type In And Replace Or

Create dynamic documents from your templates and PHP

The replace method of Docxpresso offers y'all a actually powerfull way to modify the contents of a given template allowing you to take full control of the final formatting of your business organisation report or certificate.

Although it may take you a little longer to principal all its functionality we believe that you are really going to love it because it will greatly simplify your work in multiple ocassions.

Utilise certificate templates to generate PDF

The fact that you can safely rely on your own document templates to generate documentation in PDF or whatever other available format will streamline the interconnection between the design and content producing tasks that are by and large carried out by different members of your team.

Before getting into the details we would like to summarize some of the things that you can do with a few lines of code. You may:

  • Replace placeholder variables and/or natural text past plain text, enriched text or even full document fragments.
  • Replicate table rows, list items or paragraphs with custom content.
  • Replace and resize placeholder images.
  • Use directly HTML5 code or parse line breaks.
  • Clone full sections of your document and repopulate its contents with all of the above.

Basically at that place are no limits to what you can practice to your template with the balls that the concluding document will fully respect your branding and design!!.

Well, that is enough for the marketing side of the equation an let usa become into business.

The public API of the supercede method can be summarized as follows:

Signature

public replace ($vars[, $options])

Parameters

  • $vars (type: array). This array has as keys the names of the variables we wish to supervene upon and as values arrays with the post-obit construction:
    • value (type: mixed). It can exist:
      • a string or a DocumentFragment object or
      • an array which entries are strings or DocumentFragment objects.
    • html (type: boolean, default: true). If true detects if the content is patently text or a HTML code. If given, it overwrites for that variable the html global method option if whatsoever.
    • parse-line-breaks (blazon: boolean). If true parses line breaks. If the method detects that the string is HTML the default is imitation, otherwise defaults to true. If given, it overwrites for that variable the parse-line-breaks global method option if whatsoever.
    • cake-blazon (blazon: boolean, default: imitation). If true removes and replaces the containing paragraph. If given, it overwrites for that variable the cake-type global method choice if any.
    • image (type: boolean, default: simulated). Gear up only to true if the variable name is associated with the championship of a placeholder image. If given, information technology overwrites for that variable the element option if any.
    • match (type: integer). If it is divers and if the 'value' pick is not an array but the selected match will be replaced, i.e. if match equals 1 the outset appearance of the variable volition be replaced and the others will exist ignored. If given, it overwrites for that variable the match global method option if any.
    • width (type: cord). An optional new width for prototype replacement. The format must be similar in CSS, i.e. 100px or 2cm, 3in, etcetera.
    • height (type: string). An optional new superlative for image replacement. The format must be similar in CSS, i.east. 100px or 2cm, 3in, etcetera.
  • $options (type: assortment). This array has the post-obit bachelor keys and values:
    • format (blazon: array, default: array('{{', '}}')). An array with 2 entries giving the called openning and closing symbol for template variables. The default value is array('{{', '}}') just any other combination of symbols is possible, even array(", ") that is equivalent to plain text, although this is non recommended unless strictly necessary.
    • element (type: string, default: text). The element level at which we desire to behave out the replacement, possible values are:
      • text: information technology volition just replace the variable by text,
      • paragraph: it volition clone the containing paragraph as many times as required,
      • list: it will clone the list items as many time equally required,
      • table: the table rows that comprise the placeholder variables will be cloned as many times equally required,
      • prototype: the variable name volition be searched in the championship attribute of the image.
    • html (type: boolean, default: true). If true detects if the content is plain text or a HTML code. It may be overwritten for a single value in the $vars array.
    • parse-line-breaks (type: boolean). If true parses line breaks. If the method detects that the string is HTML the default is simulated, otherwise defaults to true. It may exist overwritten for a single value in the $vars array.
    • cake-blazon (type: boolean, default: simulated). If true removes and replaces the containing paragraph. It may be overwritten for a single value in the $vars array.
    • match (type: integer). If given and if the 'value' option is not an array only the selected friction match will exist replaced, i.eastward. if match equals 1 the offset appearance of the variable volition exist replaced and the others will be ignored. It may be overwritten for a unmarried value in the $vars array.
    • target (type: string, default: certificate). Restricts the scope of replacement. The possible values are: document, header or footer.

At starting time sight the plethora of options may seem a little overwhelming. That is the cost nosotros have to pay for maximum flexibility. Nevertheless like nosotros will prove in the post-obit things are simpler than the may seem at first sight.

Repairing broken variables

Whenever using MS Word to edit your Docxpresso templates it may well happen that MS Word breaks internally the placeholder variable inserting "<span>" tags that are not visible from the Word user interface thus breaking the supercede method.

Although this problem may be easilly circumvented by rewriting the affected variables we have introduced an auxiliary repairVariable method that does the work for you lot.

So if the replace method fails for whatsoever of your variables you should add at the very beginning of your script the following line of lawmaking:

$medico = new Docxpresso\createDocument(array('template' => 'path_to_your_template.odt')); $doc->repairVariables();        

If you lot are using a placeholder varaible delimiters other than the default double curly brackets you should call the repairVariables method passing an array with the used delimiters. For example, when using double square brackets one should write instead:
$docx->repairVariables(array('[[',']]')).

Examples of apply

Although the possible scenarios of use are practically endless we will attempt in the following to offer a few examples that will give you a good starting point for more sophisticated tasks.

Let us start with a basic example that however covers all most obvious situations. We are going to replace some information in this template with the aid of the following script:

<?php /**  * This sample script replaces some content in a template  */ require_once 'pathToDOCXPRESSO/CreateDocument.inc'; $doc = new Docxpresso\createDocument(array('template' => 'template_replace_general.odt')); $format = '.pdf';//.pdf, .md, .docx, .odt, .rtf //supersede unmarried variable $medico->replace(array('variable' => assortment('value' => 'replaced text'))); //replace natural text $doc->supersede(assortment('supplant me, please' => assortment('value' => 'some other text')), assortment('format' => array('',''))); //populate the listing $doc->replace(array('detail' => assortment('value' => array('commencement', 'second', 'third'))), assortment('element' => 'list')); //populate the table $vars =array('product' => array('value' => assortment('Smart phone', 'MP3 player', 'Camera')),              'price' => array('value' => assortment('430.00', '49.99', '198,49')), ); $doc->replace($vars, array('element' => 'table'));	 //supervene upon unmarried variable past different values $medico->replace(assortment('test' => assortment('value' => assortment('one', 'two', '3')))); //and at present a variable in the header $doc->supercede(array('example_header' => array('value' => 'header text')), array('target' => 'header')); //include in the render method the path where you want your document to exist saved $doctor->return('replaced_content' . $format);  //repeat a link to the generated document repeat 'You may download the generated certificate from the link below:<br/>'; echo '<a href="' . 'replaced_content' . $format . '">Download document</a>';        

DOWNLOAD:download pdf download doc download docx download odt download rtf

Let us now slightly modify the previous script so nosotros tin perform the replacement with HTML5+CSS formatted content:

<?php /**  * This sample script replaces some content in a template using HTML lawmaking  */ require_once 'pathToDOCXPRESSO/CreateDocument.inc'; $doc = new Docxpresso\createDocument(assortment('template' => 'template_replace_general.odt')); $format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf //replace single variable $doc->supervene upon(assortment('variable' => array('value' => 'replaced <b>text</b>'))); //replace natural text $doc->supercede(array('supersede me, delight' => array('value' => '<a href="http://www.Docxpresso.com">external link</a>')), array('format' => array('',''))); //populate the list $doc->replace(array('item' => array('value' => array('outset', '<i>2d</i>', 'third'))), array('element' => 'listing')); //populate the tabular array $vars =array('product' => array('value' => array('Smart telephone', 'MP3 player', 'Photographic camera')),              'price' => array('value' => assortment('430.00', '49.99', '198,49')), ); $doc->replace($vars, array('element' => 'table'));	 //replace unmarried variable by different values $doc->supervene upon(array('test' => assortment('value' => array('one', '<span style="colour:red">two in ruby-red</span>', 'three')))); //and now a variable in the header $medico->replace(array('example_header' => array('value' => '<span way="color:red">header text as well in ruddy</span>')), array('target' => 'header')); //include in the render method the path where you want your document to be saved $doc->render('replaced_content_HTML' . $format);  //echo a link to the generated document echo 'You may download the generated document from the link beneath:<br/>'; echo '<a href="' . 'replaced_content_HTML' . $format . '">Download document</a>';        

DOWNLOAD:download pdf download doc download docx download odt download rtf

Let the states now keep with another template that includes a more sophisticated table with rowspans and formatting:

<?php /**  * This sample script replaces some content in a complex table  */ require_once 'pathToDOCXPRESSO/CreateDocument.inc'; $doc = new Docxpresso\CreateDocument(assortment('template' => 'table_template_replace.odt')); $format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf //populate the table $vars =array('name' => array('value' => array('Smart phone', 'MP3 role player <i style="color: red">(out of stock)</i>', 'Camera')), 			 'reference' => array('value' => array('345-H26-CC', '115-H27-CC', '225-J76-CD')), 			 'currency' => array('value' => array('$', '€', '$')),              'toll' => array('value' => assortment('430.00', '49.99', '198.49')), ); $doc->supervene upon($vars, array('element' => 'table'));	 //include in the render method the path where you want your document to exist saved $medico->render('replace_table' . $format);  //repeat a link to the generated document echo 'You may download the generated certificate from the link beneath:<br/>'; repeat '<a href="' . 'replace_table' . $format . '">Download certificate</a>';        

DOWNLOAD:download pdf download doc download docx download odt download rtf

We will now perform a "block" replacement that allows to substitute a placeholder variable by arbitrary content. For this purpose we will utilize the following template that includes twice the {{block_variable}} placeholder variable that will exist replaced by an HTML5+CSS tabular array:

<?php /**  * This sample script replaces "block-type" content  */ require_once 'pathToDOCXPRESSO/CreateDocument.inc'; $doctor = new Docxpresso\createDocument(array('template' => 'template_block_replace.odt')); $format = '.pdf';//.pdf, .md, .docx, .odt, .rtf //replace single variable $html =' <html>     <caput>     <style>     * {font-family: Arial; font-size: 10pt}      p {margin-lesser: 10pt}      th {background-colour: #5B9BD5; padding: 3px 7px}      th p {colour: #f6f6f6; font-weight: bold; font-size: 10pt; margin: 0}      td {padding: 3px 7px; border: 0.5pt solid #ffffff}      td p {color: #333; font-size: 10pt; margin: 0}      .squeamish {width: 12cm; margin: auto}      .firstCol {background-color: #5B9BD5;}      .firstCol p {color: #f6f6f6; font-weight: assuming; font-size: 10pt; margin: 0}      .even {background-color: #BDD6EE;}      .odd {background-color: #DEEAF6}     </style>     </head>     <body>         <table grade="overnice">             <tr>                 <thursday><p>First Column</p></th>                 <th><p>Second Cavalcade</p></th>                 <th><p>Third Cavalcade</p></th>             </tr>             <tr>                 <td class="firstCol"><p>Row ane</p></td>                 <td grade="fifty-fifty"><p>C_1_1</p></td>                 <td class="fifty-fifty"><p>C_1_2</p></td>             </tr>             <tr>                 <td form="firstCol"><p>Row ii</p></td>                 <td class="odd"><p>C_2_1</p></td>                 <td class="odd"><p>C_2_2</p></td>             </tr>             <tr>                 <td class="firstCol"><p>Row 3</p></td>                 <td class="even"><p>C_3_1</p></td>                 <td class="fifty-fifty"><p>C_3_2</p></td>             </tr>	         </tabular array>     </body> </html>'; $medico->replace(assortment('block_variable' => array('value' => $html)), array('cake-type' => truthful)); //include in the return method the path where yous want your document to be saved $doc->render('replaced_block_HTML' . $format);  //echo a link to the generated document echo 'Yous may download the generated document from the link beneath:<br/>'; echo '<a href="' . 'replaced_block_HTML' . $format . '">Download certificate</a>';        

DOWNLOAD:download pdf download doc download docx download odt download rtf

You may also check by yourself that setting the match pick to ane or 2 simply the commencement or second ocurrence of {{block_variable}} wil be replaced offering united states a high degree of flexibility when mixing the supplant and clone methods to generate capricious complex certificate content.

Replacing images

The replacement of images follows a similar pattern.

Nosotros may replace images that have been previously tagged by including the placeholder variable into the title of the image (right click the image in your document and check for the title/description attribute and insert the variable, i.due east. {{myImage}} and {{myImage_2}} in the instance at hand.

The following sample code uses this template that includes a couple of tagged images. We will supplant both:

<?php /**  * This sample script replaces some images inside the certificate  */ require_once 'pathToDOCXPRESSO/CreateDocument.inc'; $doc = new Docxpresso\CreateDocument(array('template' => 'image_template_replace.odt')); $format = '.pdf';//.pdf, .dr., .docx, .odt, .rtf //replace images $medico->replace(array('myImage' => array('value' => 'new.jpg', 'image' => truthful))); //in the second i we will alter the original paradigm dimensions $md->supercede(array('myImage_2' => array('value' => 'new_2.jpg', 'image' => true, 'width' => '400px', 'tiptop' => '200px'))); //include in the render method the path where yous want your document to be saved $doc->render('replace_image' . $format);  //echo a link to the generated document repeat 'Y'all may download the generated certificate from the link below:<br/>'; echo '<a href="' . 'replace_image' . $format . '">Download document</a>';        

DOWNLOAD:download pdf download doc download docx download odt download rtf

Beware that if you apply in your template the aforementioned "placeholder image" more than than in one case all copies volition be replaced simultaneously and so avert that practice unless that is the result y'all wish to accomplish.

Supervene upon chart data

A carefully crafted chart may require quite a lot of piece of work so it may prove useful to exist able to just replace its data without the hassle of generating the whole chart from scratch.

The Templates subpackage, to which the replace method likewise belongs, offers us the replaceChartData method that allows united states of america to change the chart information preserving the original chart options and design.

Its public API is given by:

Signature

public replaceChartData ($var, $options)

Parameters

  • $var (type: string). It should be the name used for the chart title. You lot may leave information technology empty if you adopt to select the chart via the friction match pick.
  • $options (type: assortment). This array has the following available keys and values:
    • data (type: array), it can exist an array with unlike formats:
      • pie and donut charts: assortment(     'category_1' => 3,     'category_2' => v,     'category_3' => 4.3, )
      • bar, column, area , line, scatter, (filled-)radar and column-line charts: array(     'series' => array('series_1', 'series_2'),     'category_1' => array(20,twoscore),     'category_2' => assortment(30,10),     'category_3' => assortment(12.5, 54), )
      • bubble charts: Not supported.
    • format (type: array, default: assortment('{{', '}}')). An array with two entries giving the called openning and closing symbol for template variables. The default value is array('{{', '}}') but any other combination of symbols is possible, even array(", "), although this is not recommended unless strictly necessary.
    • match (type: integer, default: i). If given but the selected match volition be replaced, i.due east. if match equals i only the first chart with the given title will be replaced.

Notice that but the chart values which categories coincide with the data array keys volition be replaced.

Let us now use this template that includes a nicely formatted chart:

<?php /**  * This sample script replaces the information from a chart  */ require_once 'pathToDOCXPRESSO/CreateDocument.inc'; $doc = new Docxpresso\CreateDocument(array('template' => 'template_replace_chartdata.odt')); $format = '.pdf';//.pdf, .physician, .docx, .odt, .rtf $data = array(      'series' => array('Series 1', 'Serial 2', 'Series 3'),     'Category 1' => assortment(ten,20, 10),      'Category two' => array(thirty,10, 30),      'Category three' => array(12.five, 50, 5),     'Category 4' => assortment(40, 10, 10), ); $doc->replaceChartData('', assortment('information' => $information)); //include in the render method the path where you lot want your document to exist saved $doc->return('replaced_chart' . $format);  //echo a link to the generated certificate repeat 'You may download the generated certificate from the link below:<br/>'; echo '<a href="' . 'replaced_chart' . $format . '">Download document</a>';        

DOWNLOAD:download pdf download doc download docx download odt download rtf

How To Make Templates Where You Can Type In And Replace Or,

Source: https://docxpresso.com/documentation-api/templates/replace-template-content/

Posted by: bradleyfreadd.blogspot.com

0 Response to "How To Make Templates Where You Can Type In And Replace Or"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel