Pregunta

Estoy usando TCPDF para generar el PDF en uno de mis proyectos. Simplemente creo un archivo HTML y se lo doy al TCPDF para manejar la generación de PDF. Pero ahora tengo algo de HTML donde se agregan varios certificados uno después del otro y quiero tener un salto de página. El salto de página debe decidirse por HTML, es decir, quiero saber si hay algún identificador en HTML que TCPDF entienda y, en consecuencia, agrega un salto de página en el PDF generado.

¿Cómo podría hacer esto?

¿Fue útil?

Solución

Estoy usando <br pagebreak="true"/>.

Método de búsqueda writeHTML y código

if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
    // check for pagebreak
    if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
        // add a page (or trig AcceptPageBreak() for multicolumn mode)
        $this->checkPageBreak($this->PageBreakTrigger + 1);
    }
    if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
            OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
        // add a page (or trig AcceptPageBreak() for multicolumn mode)
        $this->checkPageBreak($this->PageBreakTrigger + 1);
    }
}

Otros consejos

Puede usar el método AddPage () de TCPDF en combinación con explotar () y un delimitador adecuado:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8',
                 false);

// TCPDF initialization code (...)

$delimiter = '<h1>';
$html      = file_get_contents('./test.html');
$chunks    = explode($delimiter, $html);
$cnt       = count($chunks);

for ($i = 0; $i < $cnt; $i++) {
    $pdf->writeHTML($delimiter . $chunks[$i], true, 0, true, 0);

    if ($i < $cnt - 1) {
        $pdf->AddPage();
    }
}

// Reset pointer to the last page
$pdf->lastPage();

// Close and output PDF document
$pdf->Output('test.pdf', 'I');

Intenté usar

<br pagebreak="true" />

o

<tcpdf method="AddPage" />

cada uno de ellos no resultó en el inicio de una nueva página en la parte superior de la página, sino que agregó el espacio vacío completo de la página A4 entre el texto HTML. Entonces, si el texto terminó en el medio de la página y luego se insertó el salto de página, el nuevo texto se escribió desde el medio de la página siguiente. Lo que no quería.

Lo que funcionó fue esto (lo encontré aquí TCPDF forzando un nueva página ):

$pdf->writeHTML($content, true, 0, true, 0);

$pdf->AddPage();
$pdf->setPage($pdf->getPage());  

Esto ahora comienza con la escritura de texto en la parte superior de la página.

TCPDF admite el atributo 'pagebreak' para etiquetas HTML y propiedades CSS 'page-break-before' y 'page-break-after'. Por ejemplo, puede usar <br pagebreak="true" />.

Consulte el sitio web y foros oficiales http://www.tcpdf.org para obtener más información.

Con la versión 5.9.142 del 2011-12-23 podríamos usar las propiedades page-break-before, page-break-inside css, como esta:

<div style="page-break-inside:avoid;">
some non breakable text
</div>

De acuerdo a http://www.tcpdf.org/examples/example_049.phps puedes usar algo como esto

$html .= '<tcpdf method="AddPage" /><h2>Graphic Functions</h2>';

Debe verificar que el parámetro K_TCPDF_CALLS_IN_HTML en el archivo de configuración TCPDF sea verdadero.

También puede seguir este método para satisfacer sus necesidades:

$htmlcontent1="CERTIFICATE NUMBER 1 IMAGE HERE";

// output the HTML content
$pdf->writeHTML($htmlcontent1, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Print a table

// add a page
$pdf->AddPage();

$htmlcontent1="CERTIFICATE NUMBER 1 IMAGE HERE";

// output the HTML content
$pdf->writeHTML($htmlcontent1, true, 0, true, 0);
// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('textcertificate.pdf', 'D');

Espera que ayude a alguien :)

Gracias

Al darle a su elemento la propiedad page-break-after, page-break-before o page-break-inside a través de CSS, se aplicará el atributo pagebreak o pagebreakafter a la etiqueta html durante el tiempo de ejecución de TCPDF.

// page-break-inside
if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
    $dom[$key]['attribute']['nobr'] = 'true';
}
// page-break-before
if (isset($dom[$key]['style']['page-break-before'])) {
    if ($dom[$key]['style']['page-break-before'] == 'always') {
        $dom[$key]['attribute']['pagebreak'] = 'true';
    } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
        $dom[$key]['attribute']['pagebreak'] = 'left';
    } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
        $dom[$key]['attribute']['pagebreak'] = 'right';
    }
}
// page-break-after
if (isset($dom[$key]['style']['page-break-after'])) {
    if ($dom[$key]['style']['page-break-after'] == 'always') {
        $dom[$key]['attribute']['pagebreakafter'] = 'true';
    } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
        $dom[$key]['attribute']['pagebreakafter'] = 'left';
    } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
        $dom[$key]['attribute']['pagebreakafter'] = 'right';
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top