user9456654
user9456654

Reputation: 91

How to avoid page break in dompdf

I am displaying some question answers in my pdf. But in some cases, the question s on one page and answer will come to the next page. I want to avoid that situation. I want to display question and answer in same page either in first page or next page. I have tried the CSS as below:

.questiontext
{
  unbreakable: true
}

But it is not working for me. Please help me.i added pdf helper and view page as below.

$d_arr=date('Y-m-d H:i:s');
$location_name="test"       
$list_content=$this->generate_form_list($submission_id,$client_id); 


        $filename_proposalpdf="formname.pdf";
        $this->load->helper(array('dompdf', 'file'));
        pdf_create_proposal($list_content,$filename_proposalpdf,true);

in generate formlist i added view page as below:

public function generate_form_list($id,$client_id)
{
    $data['form_data']=$this->Mobile_app_model->get_form_list($id,$client_id);
    $data['client_data']=$this->Mobile_app_model->get_client_data($client_id);
    $data['client_idsss']=$client_id;
    return $this->load->view('pdf_formlist',$data,true);
}

In view page just i have displayed the data as below:

<?php
foreach($task_data as $fdata)
{ 
  echo '<p class="questiontext">';echo $fdata['question_text'];echo '</p>';
  echo '<p id="lableanswer">';echo $fdata['answer'];echo '</p>';echo '<br/>';
}
?>

Please help me how to make both are in one page.

Upvotes: 6

Views: 9482

Answers (1)

Javier Larroulet
Javier Larroulet

Reputation: 3237

The unbreakable property is used to prevent line breaks, not page breaks.

What you're looking for is:

.question 
{
  page-break-inside: avoid;
}

You can find more detailed reference in this External reference

Addressing your comment below, the problem is that you need to wrap both the question and the answer within a single element styled with page-break-inside: avoid;

.dontsplit
{
  page-break-inside: avoid;
}

<?php
foreach($task_data as $fdata)
{
  echo '<div class="dontsplit">'; 
  echo '<p class="questiontext">';echo $fdata['question_text'];echo '</p>';
  echo '<p id="lableanswer">';echo $fdata['answer'];echo '</p>';echo '<br/>';
  echo '</div>';
}
?>

If you style the dontsplit class with page-break-inside: avoid; you'll get the whole question-answer block to remain together avoiding page breaks.

Upvotes: 13

Related Questions