Esempio n. 1
0
 /**
  * Método principal del comando
  * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl)
  * @version 2015-12-15
  */
 public function main()
 {
     // cargar XML y extraer arreglo con datos de carátula y DTEs
     $EnvioDte = new \sasco\LibreDTE\Sii\EnvioDte();
     $EnvioDte->loadXML(file_get_contents($this->args['xml']));
     $Caratula = $EnvioDte->getCaratula();
     $Documentos = $EnvioDte->getDocumentos();
     // procesar cada DTEs e ir creando los archivos PDF
     foreach ($Documentos as $DTE) {
         if (!$DTE->getDatos()) {
             $this->error('No se pudieron obtener los datos de uno de los DTE del XML');
         }
         $this->out('Generando PDF para DTE ' . $DTE->getID());
         $pdf = new \sasco\LibreDTE\Sii\PDF\Dte($this->args['papel']);
         $pdf->setFooterText();
         if ($this->args['logo']) {
             $pdf->setLogo($this->args['logo']);
         }
         $pdf->setResolucion(['FchResol' => $Caratula['FchResol'], 'NroResol' => $Caratula['NroResol']]);
         $pdf->setWebVerificacion($this->args['web']);
         $pdf->agregar($DTE->getDatos(), $DTE->getTED());
         if (isset($this->args['cedible'])) {
             $pdf->setCedible(true);
             $pdf->agregar($DTE->getDatos(), $DTE->getTED());
         }
         $pdf->Output($this->args['dir'] . '/dte_' . $Caratula['RutEmisor'] . '_' . $DTE->getID() . '.pdf', 'F');
     }
     return 0;
 }
//$archivo = 'xml/certificado/etapa_simulacion.xml';
// Cargar EnvioDTE y extraer arreglo con datos de carátula y DTEs
$EnvioDte = new \sasco\LibreDTE\Sii\EnvioDte();
$EnvioDte->loadXML(file_get_contents($archivo));
$Caratula = $EnvioDte->getCaratula();
$Documentos = $EnvioDte->getDocumentos();
// directorio temporal para guardar los PDF
$dir = sys_get_temp_dir() . '/dte_' . $Caratula['RutEmisor'] . '_' . $Caratula['RutReceptor'] . '_' . str_replace(['-', ':', 'T'], '', $Caratula['TmstFirmaEnv']);
if (is_dir($dir)) {
    \sasco\LibreDTE\File::rmdir($dir);
}
if (!mkdir($dir)) {
    die('No fue posible crear directorio temporal para DTEs');
}
// procesar cada DTEs e ir agregándolo al PDF
foreach ($Documentos as $DTE) {
    if (!$DTE->getDatos()) {
        die('No se pudieron obtener los datos del DTE');
    }
    $pdf = new \sasco\LibreDTE\Sii\PDF\Dte(false);
    // =false hoja carta, =true papel contínuo (false por defecto si no se pasa)
    $pdf->setFooterText();
    $pdf->setLogo('/home/delaf/www/localhost/dev/pages/sasco/website/webroot/img/logo_mini.png');
    // debe ser PNG!
    $pdf->setResolucion(['FchResol' => $Caratula['FchResol'], 'NroResol' => $Caratula['NroResol']]);
    //$pdf->setCedible(true);
    $pdf->agregar($DTE->getDatos(), $DTE->getTED());
    $pdf->Output($dir . '/dte_' . $Caratula['RutEmisor'] . '_' . $DTE->getID() . '.pdf', 'F');
}
// entregar archivo comprimido que incluirá cada uno de los DTEs
\sasco\LibreDTE\File::compress($dir, ['format' => 'zip', 'delete' => true]);
 /**
  * Recurso de la API que genera el PDF de los DTEs contenidos en un EnvioDTE
  * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl)
  * @version 2016-01-15
  */
 public function _api_generar_pdf_POST()
 {
     // verificar si se pasaron credenciales de un usuario
     $User = $this->Api->getAuthUser();
     if (is_string($User)) {
         $this->Api->send($User, 401);
     }
     // si hubo problemas al subir el archivo error
     if (!isset($this->Api->data['xml']) and (!isset($_FILES['xml']) or $_FILES['xml']['error'])) {
         $this->Api->send('Hubo algún problema al recibir el archivo XML con el EnvioDTE', 500);
     }
     // recuperar contenido del archivo xml
     if (isset($this->Api->data['xml'])) {
         $xml = base64_decode($this->Api->data['xml']);
     } else {
         $xml = file_get_contents($_FILES['xml']['tmp_name']);
     }
     // recuperar contenido del logo (si existe)
     if (isset($this->Api->data['logo'])) {
         $logo = base64_decode($this->Api->data['logo']);
     } else {
         if (isset($_FILES['logo']) and !$_FILES['logo']['error']) {
             $logo = file_get_contents($_FILES['logo']['tmp_name']);
         }
     }
     // crear flag cedible
     $cedible = !empty($this->Api->data['cedible']) ? $this->Api->data['cedible'] : false;
     // crear flag papel continuo
     $papelContinuo = !empty($this->Api->data['papelContinuo']) ? $this->Api->data['papelContinuo'] : false;
     // crear opción para web de verificación
     $webVerificacion = !empty($this->Api->data['webVerificacion']) ? $this->Api->data['webVerificacion'] : false;
     // sin límite de tiempo para generar documentos
     set_time_limit(0);
     // Cargar EnvioDTE y extraer arreglo con datos de carátula y DTEs
     $EnvioDte = new \sasco\LibreDTE\Sii\EnvioDte();
     $EnvioDte->loadXML($xml);
     $Caratula = $EnvioDte->getCaratula();
     $Documentos = $EnvioDte->getDocumentos();
     // directorio temporal para guardar los PDF
     $dir = sys_get_temp_dir() . '/dte_' . $Caratula['RutEmisor'] . '_' . $Caratula['RutReceptor'] . '_' . str_replace(['-', ':', 'T'], '', $Caratula['TmstFirmaEnv']);
     if (is_dir($dir)) {
         \sasco\LibreDTE\File::rmdir($dir);
     }
     if (!mkdir($dir)) {
         $this->Api->send('No fue posible crear directorio temporal para DTEs', 500);
     }
     // procesar cada DTEs e ir agregándolo al PDF
     foreach ($Documentos as $DTE) {
         if (!$DTE->getDatos()) {
             $this->Api->send('No se pudieron obtener los datos de un DTE', 500);
         }
         // generar PDF
         $pdf = new \sasco\LibreDTE\Sii\PDF\Dte($papelContinuo);
         $pdf->setFooterText();
         if (isset($logo)) {
             $pdf->setLogo('@' . $logo);
         }
         $pdf->setResolucion(['FchResol' => $Caratula['FchResol'], 'NroResol' => $Caratula['NroResol']]);
         if ($webVerificacion) {
             $pdf->setWebVerificacion($webVerificacion);
         }
         $pdf->agregar($DTE->getDatos(), $DTE->getTED());
         if ($cedible and $DTE->esCedible()) {
             $pdf->setCedible(true);
             $pdf->agregar($DTE->getDatos(), $DTE->getTED());
         }
         $file = $dir . '/dte_' . $Caratula['RutEmisor'] . '_' . $DTE->getID() . '.pdf';
         $pdf->Output($file, 'F');
     }
     // si solo es un archivo y se pidió no comprimir se entrega directamente
     if (isset($this->Api->data['compress']) and !$this->Api->data['compress'] and !isset($Documentos[1])) {
         $this->response->sendFile($file, ['disposition' => 'attachement', 'exit' => false]);
         \sowerphp\general\Utility_File::rmdir($dir);
         exit(0);
     } else {
         \sasco\LibreDTE\File::compress($dir, ['format' => 'zip', 'delete' => true]);
     }
 }