Atul sen
Atul sen

Reputation: 1

I'm encountering an issue where text in my document is being cut off when it extends to two or more pages. I'm using [wkhtmltopdf] to generate pdf

I'm currently using wkhtmltopdf version 0.12.6 (with patched qt) to generate PDF documents from HTML content. However, I've encountered an issue where text gets cut off when it spans multiple pages. This problem occurs specifically within a Continuous Integration (CI) environment.

When generating PDFs, if the content extends beyond a single page, portions of the text are truncated or missing on subsequent pages. This issue disrupts the readability and completeness of the generated documents, affecting the overall user experience.

I've attached an image that illustrates this problem. In the image, you can see how text is abruptly cut off at the bottom of the first page and does not continue onto the next page as expected.

I've tried adjusting various parameters and configurations in wkhtmltopdf, but haven't found a solution yet. The problem persists consistently across different HTML documents and content layouts.

Any insights or suggestions on how to resolve this issue, particularly within a CI environment, would be greatly appreciated.[enter image description here](https://i.sstatic.net/NLWxl.png)

public function download_template($id='') {

// Set your HTML content or load it from a file

    if(!empty($id)){
        $template = $this->db->select('*')->where('id',$id)->get('resume')->row_array();

        $user = $this->db->where('id', $template['user_id'])->get('user')->row_array();
        
        ini_set('memory_limit', '   5048M');

        $this->load->library('pdf');
        $html = $template['section_data'];
                        $html =  'htmlcontent ';
        $orderid='1';
        $o=rand();
        $imageFolder = './uploads/test'; // Replace with your desired folder name
        $imageFilename = $imageFolder . '/' . $o . '.html';
 
        // Ensure the folder exists or create it if it doesn't
        if (!is_dir($imageFolder)) {
            mkdir($imageFolder, 0777, true);
        }

        file_put_contents($imageFilename, $html);

         $outputDirectory ='./uploads/test'; 
        $imageFile = $outputDirectory . '/' . $o . 'generated.pdf';

        exec("wkhtmltopdf $imageFilename $imageFile 2>&1", $output, $returnCode);

        $imageFilename = $imageFilename;
        $imageFile =$imageFile;
        // print_r($imageFilename);
        // die();
        // Set all margins to zero
        $margins = 0;
        $margins1 = 4;
        $margins2 =6;
        $zoomFactor = 1.5;

        $footerHtmlFile = 'footer.html';

        $command = "wkhtmltopdf   --footer-html $footerHtmlFile  --encoding UTF-8  --margin-top $margins1 --margin-bottom $margins2 --margin-left $margins --margin-right $margins --zoom $zoomFactor $imageFilename $imageFile 2>&1 ";

        exec($command, $output, $returnCode);

        if ($returnCode === 0) {
         
             header('Content-Type: application/pdf');
            header('Content-Disposition: attachment; filename="'.$user['username'].'.pdf"');
            
            // Read the file and output it to the browser
            readfile($imageFile);

            // Optional: Delete the generated PDF file after download
            unlink($imageFile);
        } else {
            echo "PDF generation failed. Output: " . implode("\n", $output);
        }

        die;


        if ($returnCode === 0) {
            // Set headers for PDF download
            header('Content-Description: File Transfer');
            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment; filename="' . basename($imageFile) . '"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($imageFile));

            
            // Output the PDF file
            readfile($imageFile);
        
            // Optionally, you can delete the generated file after download
            unlink($imageFile);
        
            exit;
        } else {
            echo "Conversion failed. Error output: " . implode("\n", $output);
        }
        exit;

     
        unlink($imageFilename);
   
       
        $this->pdf->loadHtml($html);
        $this->pdf->setPaper('A4', 'portrait');
        $this->pdf->render();



        // $this->pdf->stream(1);
        // Output the generated PDF (1 = download and 0 = preview)
        $file_name = time();
        if(isset($user['username'])) {
            $file_name = str_replace(' ', '_', $user['username']) . '_' .date('Ymd') . time();
        }
             $data = $this->pdf->stream($file_name . ".pdf", array("Attachment"=> 1));
        }
    }

Upvotes: 0

Views: 357

Answers (0)

Related Questions