как сохранить сгенерированный DOMPDF контент в файл? [закрыто]
Я использую Dompdf для создания PDF-файла, но я не знаю, почему он не сохраняет созданный PDF-файл на сервере.
Есть идеи?
require_once("./pdf/dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
file_put_contents('Brochure.pdf', $dompdf->output());
3 answers
Я только что использовал dompdf, и код был немного другим, но он работал.
Вот оно:
require_once("./pdf/dompdf_config.inc.php");
$files = glob("./pdf/include/*.php");
foreach($files as $file) include_once($file);
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('Brochure.pdf', $output);
Единственное отличие здесь в том, что включены все файлы в каталоге include.
Кроме этого, моим единственным предложением было бы указать полный путь к каталогу для записи файла, а не только имя файла.
Я проверил ваш код, и единственной проблемой, которую я увидел, было отсутствие разрешения, предоставленного каталогу, в который вы пытаетесь записать файл.
Дайте разрешение "запись" в каталог, в который вам нужно поместить файл. В вашем случае это текущий каталог.
Используйте "chmod" в linux.
Добавьте "Все" с включенной функцией "запись" на вкладку "Безопасность" каталога, если вы находитесь в Windows.
<?php
$content='<table width="100%" border="1">';
$content.='<tr><th>name</th><th>email</th><th>contact</th><th>address</th><th>city</th><th>country</th><th>postcode</th></tr>';
for ($index = 0; $index < 10; $index++) {
$content.='<tr><td>nadim</td><td>[email protected]</td><td>7737033665</td><td>247 dehligate</td><td>udaipur</td><td>india</td><td>313001</td></tr>';
}
$content.='</table>';
//$html = file_get_contents('pdf.php');
if(isset($_POST['pdf'])){
require_once('./dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF;
$dompdf->load_html($content);
$dompdf->render();
$dompdf->stream("hello.pdf");
}
?>
<html>
<body>
<form action="#" method="post">
<button name="pdf" type="submit">export</button>
<table width="100%" border="1">
<tr><th>name</th><th>email</th><th>contact</th><th>address</th><th>city</th><th>country</th><th>postcode</th></tr>
<?php for ($index = 0; $index < 10; $index++) { ?>
<tr><td>nadim</td><td>[email protected]</td><td>7737033665</td><td>247 dehligate</td><td>udaipur</td><td>india</td><td>313001</td></tr>
<?php } ?>
</table>
</form>
</body>
</html>