/
gerarPDF.php
executable file
·67 lines (49 loc) · 1.73 KB
/
gerarPDF.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';
if(!isset($_SESSION['PATH_SYS'])){
require_once '_loadPaths.inc.php';
}
$path = $_SESSION['PATH_SYS'];
include_once($path['controller'].'EnvioDocumentoController.php');
$envioDocumentoControler = new EnvioDocumentoController();
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$param = $_SERVER['QUERY_STRING'];
$host = $_SERVER["HTTP_HOST"];
$opts = Array(
"http" => Array(
"method" => "POST",
"header" => "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($param) . "\r\n",
"content" => $param
)
);
$context = stream_context_create($opts);
$folder = (substr($_SERVER["HTTP_HOST"],0,5) == "local") ? "Hospital/" : "";
$file = file_get_contents("http://{$host}/{$folder}pesquisa_pdf.php",false,$context);
$dompdf->load_html($file);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
$arquivo = $dompdf->output("arquivo.pdf", array("Attachment" => true));
$rand = rand(1,500);
$rand2 = rand(1,500);
$nomeCrip = md5("arquivo".$rand.$rand2);
if($arquivo){
file_put_contents($path['arquivos'].$nomeCrip.'.pdf',$arquivo);
$env = new EnvioDocumento();
$env->setEnv_idEscola($_SESSION['idEscolaPre']);
$env->setEnv_idRemetente(1);
$env->setEnv_idDestinatario(4);
$env->setEnv_url($nomeCrip.'.pdf');
$env->setVisto(0);
$envioDocumentoControler->insert($env);
echo json_encode(["status" => "Arquivo gerado com sucesso"]);
} else {
throw new Exception("Ocorreu um erro ao gerar o arquivo pdf.");
}
?>