2009-08-25 12 views
9

Tengo un problema, con líneas múltiples Zend_Pdf, mi problema es que me no puedo escribir todo el texto a mi texto pdf.My se parece a esto: http://pastebin.com/f6413f664problemas PDF de varias líneas de Zend Framework

Pero cuando abro mi archivo .pdf, el texto es el siguiente: http://screencast.com/t/1CBjvRodeZQd

y aquí es mi código:

public function pdfAction() 
{ 
    $this->_helper->layout->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(); 

    $theID = ($this->_getParam('id') !== NULL) ? (int)$this->_getParam('id') : false; 

    ($theID === false) ? $this->_redirect('/home') : false; 

    //Information 
    $info = $this->artists->artistInfo($theID); 

    // Create new PDF 
    $pdf = new Zend_Pdf(); 
    $pdf->properties['Title'] = "TITLE"; 
    $pdf->properties['Author'] = "AUTHOR"; 

    // Add new page to the document 
    $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); 
    $pdf->pages[] = $page; 

    // Set font 
    $page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 8); 

    // Draw text 
    foreach (explode("</p>", $info[0]['biography']) as $i => $line) { 
     $page->drawText($line, 0, 820 - $i * 10, 'UTF-8'); 
    } 

    $this->getResponse()->setHeader('Content-type', 'application/x-pdf', true); 
    $this->getResponse()->setHeader('Content-disposition', 'attachment; filename=my-file.pdf', true); 
    $this->getResponse()->setBody($pdf->render()); 
} 

lo que yo quiero hacer, es que cada <p> para tomarse un descanso (<br /> \ n) y para mostrar todo el texto.

¿Alguna solución chicos?

Respuesta

17

lugar de esto:

// Draw text 
foreach (explode("</p>", $info[0]['biography']) as $i => $line) { 
    $page->drawText($line, 0, 820 - $i * 10, 'UTF-8'); 
} 

le daría prueba (no lo probamos):

// Draw text  
$charsPerLine = 50; 
$heightPerLine = 10; 

$text = str_replace('<p>','',$info[0]['biography']); 
$lines = array(); 

foreach (explode("</p>", $text) as $line) { 
    $lines = array_merge(
        $lines, 
        explode(
          "\n", 
          wordwrap($line, $charsPerLine, "\n") 
        ) 
    ); 
} 

foreach ($lines as $i=>$line) { 
    $page->drawText($line, 0, 820 - $i * $heightPerLine, 'UTF-8'); 
} 

Obviamente, se necesita para jugar con esos 2 "constantes" para obtener los mejores resultados .

Espero que ayude.

+0

Sí, funciona, pero tengo una pregunta más, ¿cómo puedo poner en la parte superior antes del texto, un título? – Uffo

+0

cambie el 820 en el código a ~ 800 y agregue otra llamada a drawText para imprimir su título –

+0

¡¡Muchísimo hombre !! – Uffo