Sumario to PDF with Dompdf/dompdf

Asked

Viewed 65 times

0

I am using DOMPDF to compile my reports, however I can not relate in summary the contents of respective topics.

 session_start();
    
    require  __DIR__."/vendor/autoload.php";
    use Dompdf\Dompdf; 
    use Dompdf\Options;
    $options = new Options();
    $options->setIsPhpEnabled(true);
    $dompdf = new Dompdf($options);
    
    $sumario = '';
    foreach($_SESSION['sumario'] as $index=>$titulo){
        $sumario .=   "<div>";                
            $sumario .="<span>{$index}. {$titulo}</span>";
            $sumario .="<p>x</p>"; // Aqui mora o problema, não consigo relacionar este valor!          
        $sumario .="</div>";
    }
    ob_start();
        $documento =  $_SESSION['documento'];
        require __DIR__."/pdf.php";
    $pdf = ob_get_clean();
    
   
    //echo $pdf; die;
    
    $dompdf->loadHtml($pdf);

    $dompdf->setPaper("A4");
    $dompdf->render();
    $dompdf->stream("file.pdf",["Attachment" => false]);

I did it in a very simple way, the $_SESSION['document'] is basically an html file, where the 'H1' are the topics, the subtopic 'H2' and 'p' are the common texts.

The $_SESSION['sumario'] was something I did in desperation, trying to make the sumario. It’s like this :

$_SESSION['sumario'] = array(
       [1] => 'Titulo 01',
       [2] => 'Titulo 02',
       [3] => 'Titulo 03
);

Both sessions infected in "pdf.php";

1 answer

0

I don’t know if I understand you but you need to link to the summary that takes the page in question, right? If so, you need to anchor your links.

<!-- Links do sumário deverão ser dessa forma -->
<a href="#pagina1"> Ir para página 1 </a>

<!-- coloque uma âncora em cada página, o número ou título da página, por exemplo  -->
<a name="pagina1"> 1 </a>

If it doesn’t work you can try to replace the <a name> by some element with the same ID

<span id="pagina1"></span>

  • Your example will suit me, but that’s not what I need. I think I was a little confused on the question, so I edited it to try to be more precise. What I need is number the summary with the pages of each topic.

  • I still can not understand. What would be the "X" in <p>x</p>?

  • This is where I would put the topic page. For example if Title 01 were on page 02, it would be <span>1. Title 01</span><p>02</p>";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.