skip to Main Content

I have six html pages in the single html file and ı want to convert to pdf with dompdf like below;

  1. page size landscape,
  2. page size landscape,
  3. page size landscape,
  4. page size portrait,
  5. page size portrait,
  6. page size portrait

how can I do?

I have six html pages in the single html file and ı want to convert to pdf with dompdf

2

Answers


  1. Chosen as BEST ANSWER
    <!--First Page-->
    <div class="container">
        
        Page 1
    </div>
    
    <div class="container">
        
        Page 2
    </div>
    
    <div class="container">
        
        Page 3
    </div>
    

    . . . .

    this is my php page. I want to convert this php page as pdf as shown below.

    1. page size landscape,
    2. page size landscape,
    3. page size landscape,
    4. page size portrait,
    5. page size portrait,
    6. page size portrait

  2. Use Composer to install dompdf.

    composer require dompdf/dompdf
    

    Create a new PHP file, name it convert.php, and include the dompdf library.

    require_once 'vendor/autoload.php';
    use DompdfDompdf;
    

    Instantiate the Dompdf class.

    $dompdf = new Dompdf();
    

    Set the paper size and orientation for each HTML page.

    $pages = [
        ['html' => 'sayfa1.html', 'size' => 'landscape'],
        ['html' => 'sayfa2.html', 'size' => 'landscape'],
        ['html' => 'sayfa3.html', 'size' => 'landscape'],
        ['html' => 'sayfa4.html', 'size' => 'portrait'],
        ['html' => 'sayfa5.html', 'size' => 'portrait'],
        ['html' => 'sayfa6.html', 'size' => 'portrait']
    ];
    
    foreach ($pages as $page) {
        $dompdf->set_paper($page['size']);
        $html = file_get_contents($page['html']);
        $dompdf->load_html($html);
        $dompdf->render();
    }
    

    PDF File saves.

    $dompdf->stream('cikti.pdf', ['Attachment' => false]);
    

    Run the script by executing the following command in the terminal.

    php convert.php
    

    Hello.

    Update 🙂

    require_once 'vendor/autoload.php';
    use DompdfDompdf;
    
    $dompdf = new Dompdf();
    
    $pages = [
        ['html' => 'sayfa1.html', 'size' => 'landscape'],
        ['html' => 'sayfa2.html', 'size' => 'landscape'],
        ['html' => 'sayfa3.html', 'size' => 'landscape'],
        ['html' => 'sayfa4.html', 'size' => 'portrait'],
        ['html' => 'sayfa5.html', 'size' => 'portrait'],
        ['html' => 'sayfa6.html', 'size' => 'portrait']
    ];
    
    foreach ($pages as $page) {
        $dompdf->setPaper('A4', $page['size']);
        $html = file_get_contents($page['html']);
    
        // HTML Sayfasında php etiketi var ise kaldıralım.
        $html = preg_replace('/<?php.*??>/s', '', $html);
    
        $dompdf->loadHtml($html);
        $dompdf->render();
    
        $outputFilename = basename($page['html'], '.html') . '.pdf';
        file_put_contents($outputFilename, $dompdf->output());
    }
    

    Make sure you have the files like sayfa1.html in the directory where the page is located, then when you run php convert.php in the terminal, you can generate a PDF file for each HTML page.

    Sorry google translate 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search