79 lines
2.3 KiB
PHP
79 lines
2.3 KiB
PHP
<?php
|
|
function xlsx_to_html($xlsx, $html_dir) {
|
|
// Ensure the LANG environment variable is set
|
|
putenv('LANG=ru_RU.UTF-8');
|
|
|
|
// Ensure the output directory exists
|
|
if (!is_dir($html_dir)) {
|
|
mkdir($html_dir, 0777, true);
|
|
}
|
|
|
|
// Construct the command
|
|
$command = "sudo -u root libreoffice --headless --convert-to html --outdir " . escapeshellarg($html_dir) . " " . escapeshellarg($xlsx);
|
|
// Execute the command
|
|
$output = shell_exec($command);
|
|
$output = extract_between_markers($output);
|
|
|
|
update_src_paths($output, $html_dir);
|
|
|
|
return $output;
|
|
}
|
|
|
|
function extract_between_markers($input_str) {
|
|
// Define the regular expression pattern to match the content between the markers
|
|
$pattern = '/->\s*(.*?)\s*using filter : HTML \(StarCalc\)/';
|
|
|
|
// Perform the regex match
|
|
if (preg_match($pattern, $input_str, $matches)) {
|
|
// Return the matched content
|
|
return $matches[1];
|
|
}
|
|
|
|
// Return null if no match is found
|
|
return null;
|
|
}
|
|
|
|
function update_src_paths($htmlPath, $newBasePath) {
|
|
// Load HTML content from the file
|
|
if(is_null($htmlPath))
|
|
{
|
|
dd("htmlPath null dönüyor");
|
|
}
|
|
$htmlContent = file_get_contents($htmlPath);
|
|
|
|
// Create a new DOMDocument instance
|
|
$dom = new DOMDocument();
|
|
|
|
// Suppress warnings due to malformed HTML
|
|
libxml_use_internal_errors(true);
|
|
|
|
// Load HTML into the DOMDocument
|
|
$dom->loadHTML($htmlContent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
|
|
|
// Restore error handling
|
|
libxml_clear_errors();
|
|
|
|
// Get all 'src' attributes
|
|
$xpath = new DOMXPath($dom);
|
|
$srcAttributes = $xpath->query('//img/@src | //script/@src | //link/@href');
|
|
|
|
foreach ($srcAttributes as $attribute) {
|
|
$currentValue = $attribute->value;
|
|
// Extract the current path and replace it with the new base path
|
|
$updatedValue = url($newBasePath) . "/" . $currentValue;
|
|
$attribute->value = $updatedValue;
|
|
}
|
|
|
|
// Save the updated HTML back to the file
|
|
$updatedHtmlContent = $dom->saveHTML();
|
|
|
|
chmod("storage/documents/", 0777);
|
|
unlink($htmlPath);
|
|
|
|
if (!file_exists(dirname($htmlPath))) {
|
|
mkdir(dirname($htmlPath), 0777, true);
|
|
}
|
|
|
|
file_put_contents($htmlPath, $updatedHtmlContent);
|
|
}
|
|
?>
|