Forum


Replies: 5   Views: 232
How to add url link in text type placeholder
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by fairshareitservices  · 13-01-2025 - 06:26

Hello

how to add url to text type placeholder. Text is replaced in word document from excel file. Like we used <b> </br> tag in same text can we use <link href="">??

 

Thank You

Posted by fairshareitservices  · 13-01-2025 - 11:33

We are not replacing using htmltemplate. Genrating document using  $docx = new CreateDocx();

Posted by admin  · 13-01-2025 - 11:40

Hello,

Our previous reply can be used with documents created from scratch and DOCX templates.

Links can be added using embedHTML and addLink methods. And also cross-references with addCrossReference.

Regards.

Posted by fairshareitservices  · 14-01-2025 - 05:48

Below is our function to add link to a placeholder - 

$fomattedURLWordFragment = addUrlLink($value,$templateDocx,$placeHolderName);
$templateDocx->replaceVariableByWordFragment(array('placeHolderName' => $fomattedURLWordFragment), array('type' => 'inline', 'target' => 'document'));

function addUrlLink($inputText,$templateDocx,$placeHolderName){ Â  Â 
    echo "In addUrlLink";
    $docx = new CreateDocx();
    $wf = new WordFragment($docx);

    //preg_match('/<link>(.*?)<\/link>/', $inputText, $matches);
    
    $text = array();
    //print_r($matches); //die();
    //$url = $matches[1]; // This will contain 'www.hdfcfund.com'
    $url = 'www.hdfcfund.com';
    $link = new WordFragment($docx);
    //$link->addLink($matches[0], array('url'=> $url, 'color' => '0000FF', 'u' => 'single'));
    $link->addLink($inputText, array('url'=> $url, 'color' => '0000FF'));
    $text[] = $link;
    
    //print_r($text); //die();
    $wf->addText($text, array('font' => 'Cambria', 'fontSize' => 6.5));
    return $wf;

}

This does not add link to the placeholder variable. Please correct the above code.

Posted by admin  · 14-01-2025 - 07:13

Hello,

Please check and run the following sample included in the package: examples/Templates/replaceVariableByWordFragment/sample_2.php . This sample replaces a placeholder in a template with a new WordFragment that includes an image, a link, and a footnote.

The problem with your script is that it's using a new CreateDocx object instead of the DOCX template object to generate the WordFragment. Instead of:

$docx = new CreateDocx();
$wf = new WordFragment($docx);

it needs to use the DOCX template object, not a new object:

$wf = new WordFragment($templateDocx);

Also note that if you want to add a link to a URL you need to use http or https:

$url = 'https://www.hdfcfund.com';

otherwise, a local link is generated.

Regards.