Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
172 righe
6.2 KiB
172 righe
6.2 KiB
<? |
|
use Dompdf\Dompdf; |
|
use Dompdf\Options; |
|
|
|
class documentConverter { |
|
|
|
protected static function getTMPName() { |
|
return sys_get_temp_dir() . "/" . generateStrongPassword(16,false,"lud") . time(); |
|
} |
|
|
|
private static function setPDFProfile() : ?String { |
|
global $config; |
|
$path = $config["mediaFolder"] . "/pdfa_def.ps"; |
|
if (!file_exists($path)) { |
|
$profile = file_get_contents(__DIR__."/pdfProfile/pdfa_def.ps"); |
|
$srgb = file_get_contents(__DIR__."/pdfProfile/srgb.icc"); |
|
$srgbPath = $config["mediaFolder"] . "/srgb.icc"; |
|
if (file_put_contents($srgbPath,$srgb)) { |
|
$profile = str_replace("(srgb.icc)","({$srgbPath})",$profile); |
|
file_put_contents($path,$profile); |
|
} |
|
} |
|
if (file_exists($path)) { |
|
return $path; |
|
} |
|
} |
|
|
|
public static function convertPDFtoPDFA($pdfPath) { |
|
if (file_exists($pdfPath)) { |
|
$profilePath = self::setPDFProfile(); |
|
if (!empty($profilePath)) { |
|
$cmd = "gs -dNOSAFER -sDEVICE=pdfwrite -dPDFA=1 -sProcessColorModel=DeviceRGB -sColorConversionStrategy=RGB -dPDFACompatibilityPolicy=1 -sOutputFile={$pdfPath}-a.pdf \"{$profilePath}\" \"{$pdfPath}\""; |
|
exec($cmd); |
|
if (file_exists("{$pdfPath}-a.pdf")) { |
|
$content = file_get_contents("{$pdfPath}-a.pdf"); |
|
unlink("{$pdfPath}-a.pdf"); |
|
return $content; |
|
} |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
public static function cleanHTML($html) { |
|
return iconv('UTF-8', 'UTF-8//IGNORE', $html); |
|
} |
|
|
|
public static function getPDFWithDOMPDF($html,$format="A4",$orientation="portrait",$pdfA=true) { |
|
$options = new Options(); |
|
$options->setIsRemoteEnabled(true); |
|
$options->set('defaultFont', 'Helvetica'); |
|
$options->set("isPhpEnabled", true); |
|
$options->set('enable_html5_parser',true); |
|
$dompdf = new Dompdf($options); |
|
$dompdf->loadHtml($html); |
|
$dompdf->setPaper($format, $orientation); |
|
$dompdf->render(); |
|
$content = $dompdf->output(); |
|
if ($pdfA) { |
|
$tmp = self::getTMPName(); |
|
file_put_contents($tmp,$content); |
|
unset($content); |
|
if (file_exists($tmp)) { |
|
$content = self::convertPDFtoPDFA($tmp); |
|
unlink($tmp); |
|
} |
|
} |
|
if (!empty($content)) { return $content; } |
|
return false; |
|
} |
|
|
|
public static function getPDFWithTCPDF($html,$format="A4",$orientation="portrait",$pdfA=true,$footer = false) { |
|
switch($orientation) { |
|
case "landscape": $orientation = "L"; break; |
|
default: $orientation = "P"; |
|
} |
|
if ($footer) { |
|
$class= "TCPDFwFooter"; |
|
} else { |
|
$class= "TCPDF"; |
|
} |
|
$pdf = new $class($orientation, PDF_UNIT, $format, true, 'UTF-8', false, true); |
|
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); |
|
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); |
|
$pdf->SetFont('helvetica', '', 8, '', true); |
|
if ($footer) { |
|
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); |
|
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
|
} |
|
$pdf->AddPage(); |
|
$pdf->WriteHTML(utf8_encode($html)); |
|
$pdf->lastPage(); |
|
$tmp = self::getTMPName(); |
|
$pdf->Output($tmp,"F"); |
|
if (file_exists($tmp)) { |
|
if ($pdfA) { |
|
$content = self::convertPDFtoPDFA($tmp); |
|
} else { |
|
$content = file_get_contents($tmp); |
|
} |
|
unlink($tmp); |
|
if (!empty($content)) { return $content; } |
|
return false; |
|
} |
|
} |
|
|
|
public static function getPDFWithWKHTML($html,$format="A4",$orientation="portrait",$pdfA=true) { |
|
return self::getPDFWithDOMPDF($html,$format,$orientation,$pdfA); |
|
$html = self::cleanHTML($html); |
|
$tmp = self::getTMPName(); |
|
file_put_contents($tmp.".html",$html); |
|
if (file_exists($tmp.".html")) { |
|
$cmd = "wkhtmltopdf -s {$format} -O ".ucfirst($orientation)." {$tmp}.html {$tmp}.pdf"; |
|
exec($cmd); |
|
unlink($tmp.".html"); |
|
if (file_exists($tmp.".pdf")) { |
|
if($pdfA) { |
|
$content = self::convertPDFtoPDFA($tmp.".pdf"); |
|
} else { |
|
$content = file_get_contents($tmp.".pdf"); |
|
} |
|
unlink($tmp.".pdf"); |
|
if (!empty($content)) { return $content; } |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
public static function getPHPOfficeInstance($html) { |
|
$html = self::cleanHTML($html); |
|
$dom = new DOMDocument('1.0', 'UTF-8'); |
|
$dom->preserveWhiteSpace = false; |
|
$internalErrors = libxml_use_internal_errors(true); |
|
$dom->loadHTML($html,LIBXML_NOERROR); |
|
libxml_use_internal_errors($internalErrors); |
|
$dom->formatOutput = true; |
|
$html = $dom->saveXML($dom->documentElement); |
|
$phpWord = new \PhpOffice\PhpWord\PhpWord(); |
|
$section = $phpWord->addSection(); |
|
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html); |
|
return $phpWord; |
|
} |
|
|
|
public static function generateWord($html,$fileName = "export") { |
|
$phpWord = static::getPHPOfficeInstance($html); |
|
header("Content-Description: File Transfer"); |
|
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); |
|
header('Content-Transfer-Encoding: binary'); |
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
|
header('Expires: 0'); |
|
header('Content-Disposition: attachment;filename="'.$fileName.'.docx"'); |
|
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); |
|
$objWriter->save('php://output'); |
|
return true; |
|
} |
|
|
|
public static function generateODT($html,$fileName = "export") { |
|
$phpWord = static::getPHPOfficeInstance($html); |
|
header("Content-Description: File Transfer"); |
|
header("Content-Type: application/vnd.oasis.opendocument.text"); |
|
header('Content-Transfer-Encoding: binary'); |
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
|
header('Expires: 0'); |
|
header('Content-Disposition: attachment;filename="'.$fileName.'.odt"'); |
|
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText'); |
|
$objWriter->save('php://output'); |
|
return true; |
|
} |
|
} |
|
|
|
|
|
?>
|