Forum


Replies: 3   Views: 282
Insert svg file in html
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  · 06-01-2025 - 08:21

 we are generating word doument using HTML template . need svg image instead of jpg file . But below code is not showing svg file. 

foreach ($result as $row) {
                        # Add each row data into the array in the desired format
                        $fundsData[] = [
                                $row['scheme_name'],  # Scheme name
                                $row['benchmark'],  # Benchmark
                                $row['product_labelling'],  # product labelling
                                $row['benchmark_risk_image'],
                                $row['scheme_risk_image'] # Full path for benchmark risk image
                        ];
                }
                $rows = generateRows($fundsData,$col_count);
                $BenchmarkTableHtml = str_replace('$rowsPlaceholderNoPrc$', $rows, $BenchmarkTableWith4ColsHtml);
                $currentBenchmarkTableWithoutPRCRowHtml = $BenchmarkTableHtml;
                #End without prc image code 
                
                $fileName = 'benchmark_riskometer_BENCHMARKMF';
                
                //get footer date details from tag definition file. 
                //Get Mapping table columns and row index of scheme code
                $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

                $mappingSpreadsheet = $reader->load("$INPUT_DIRECTORY/scheme_tag_definition.xlsx");

                $mappingSpreadsheetData=$mappingSpreadsheet->getActiveSheet()->toArray();

                # code to actual generate doc file. 
                        if($templateType == 'MF'){
                                        $template_benchmark_file_name = 'benchmark_riskometer_template';
                                        mLog("Generate -> $OUTPUT_DIRECTORY/$fileName");
                                        $docx = new CreateDocxFromTemplate("$INPUT_DIRECTORY/$template_benchmark_file_name".'.docx');
                                        
                                        //replace footer date 
                                        $variableDetails = getDetailsOfTheVariableFromMappingTable('footer_date', $mappingSpreadsheetData);
                                        
                                        $_SESSION['session_placeholder_name'] = 'footer_date';
                                        $docx = replacePlaceHolderByText('footer','NOCODE', $docx,$variableDetails,'');
                                                                   
                                        //replace footer logo
                                        $variableDetails = getDetailsOfTheVariableFromMappingTable('mf_footer_logo', $mappingSpreadsheetData);
                                        
                                        $_SESSION['session_placeholder_name'] = 'mf_footer_logo';
                                        $docx = replacePlaceHolderByImage('footer','NOCODE', $docx,$variableDetails,'');
                        
                                        $docx->replaceVariableByHTML("benchmark_table_prc", 'block', $currentBenchmarkTableRowHtml, array('customListStyles' => true, 'isFile' => false, 'parseDivsAsPs' => true, 'downloadImages' => false));
                                        $docx->replaceVariableByHTML("benchmark_table", 'block', $currentBenchmarkTableWithoutPRCRowHtml, array('customListStyles' => true, 'isFile' => false, 'parseDivsAsPs' => true, 'downloadImages' => false));
                                        $docx->createDocx("$OUTPUT_DIRECTORY/$fileName");
                                
                        }


# Generate rows dynamically
function generateRows($data,$col_count) {
    //print_r($data);die();
    
    $rowsHtml = '
    <style>td {border:0.8px solid #000000; text-align:left; vertical-align:middle;}</style>';


    foreach ($data as $fund) {
        $rowsHtml .= '<tr style="border:0.8px solid #000000;height:110px;">';
        
        # First column contains an image for benchmark
        $rowsHtml .= '<td style="text-align:left;font-size:10px;vertical-align: top;"><span style="font-size:12px;color:#0C4C8F">'.$fund[0].'</span><br><span><b>BENCHMARK : </b></span><span style="font-size:9px;">'.$fund[1].'</span>
        </td>';
        
        # Second column contains text for the scheme name
        $rowsHtml .= '<td text-align:left;vertical-align: top;>' . $fund[2] . '</td>';
        
        # Third column contains an image for the riskometer
        $rowsHtml .= '<td style="font-size:8px;"><span>.</span><img type="image/svg+xml" style="display: block; margin: 0 auto;" src="https://devfactsheet.atilagu.com/benchmark_images/' . $fund[3] . '" alt="Riskometer Image" width="300" height="165"></td>';
        
        $rowsHtml .= '<td style="font-size:8px;"><span>.</span><img type="image/svg+xml" style="display:block; margin:0 auto;" src="https://devfactsheet.atilagu.com/benchmark_images/' . $fund[4] . '" alt="Benchmark Image" width="300" height="165"></td>';
        
         if($col_count == 5){
        # Fourth column contains an image for potential risk class
        $rowsHtml .= '<td style="font-size:8px;"><span>.</span></br></br><img style="display: block; margin: 0 auto;" src="https://devfactsheet.atilagu.com/benchmark_images/' . $fund[5] . '" alt="Potential Risk Class Image" width="250" height="130"></td>';
        }
        $rowsHtml .= '</tr>';
    }

    return $rowsHtml;
}Â 

 

Posted by fairshareitservices  · 06-01-2025 - 10:46

Please share a basic example used with the library. 

Posted by admin  · 06-01-2025 - 10:57

Hello,

Please note that you can find samples using all the features of phpdocx on the API pages. For example, on  the img tag documentation page: https://www.phpdocx.com/htmlapi-documentation/html-standard/insert-image-Word-document-with-HTML

The following simple sample adds an SVG image transforming HTML:

$html = '<img src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/mt.svg">';
$docx->embedHTML($html);

You can also use base64 content (all licenses) or an svg tag (with a Premium license).

As explained in our previous reply, PHP ImageMagick must be installed and enabled to add SVG content.

Regards.