I am using views_pdf to generate a pdf from a Drupal book (using the core book module). The book is a collection of nodes with a hierarchy of different nodes/chapters. When I first developed this I was using views_pdf 7.x-1.0 it worked fine. I had the page break field in my view set to exclude the page break from the last row so a page break was placed after the content of every node was rendered to the pdf, except for the very last node so I there wasn't a blank page at the end of the pdf. It all worked great and everyone was happy.

However, after upgrading to 7.x-1.3 this has now broken and content gets overwritten because the page break is not generated. I have narrowed the problem code down to function render() in views_pdf/field_plugins/views_pdf_handler_page_break.inc. This is the code that is not working:

if ($this->options['last_row'] == TRUE && ($this->countRecords + 1 >= $this->view->total_rows)) {
  return '';
}

After putting in some debug code to figure out what was going wrong, I found that $this->countRecords was always set to be the same number as the number of records in the view. So if I had 18 records/nodes in the view, $this->countRecords was set to 18 which meant that the above statement ALWAYS evaluated to true, causing the page break to never being written to the pdf. Since countRecords is initialised to 0 at the start of the file, I cannot work out how or why it is getting reset.

The only way I have been able to get around this problem is to do this:
1. uncheck the "Exclude from last row flag" in my view for the page break field. This avoids the above section of code and generates the page breaks as before, but it does leave a blank page at the end of the pdf.
2. to remove the blank page at the end of the pdf, I have added this code to function close() in views_pdf_template.php

	if ($this->options['last_row'] == FALSE) {
	  $this->deletePage($this->getNumPages());
	}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

johnlaine’s picture

I was having this same issue, a blank page at the end of the PDF. Your solution worked for me, Thanks!
I am receiving this warning though:

Notice: Undefined property: PdfTemplate::$options in PdfTemplate->Close()

But other than that it seems to be working.

jaime@gingerrobot.com’s picture

OK You have the right spot. However last_row will always be FALSE in that function. I am not sure how to check that the last row is set. But thanks so much for the deletePage function.

jaime@gingerrobot.com’s picture

jaime@gingerrobot.com’s picture

OK I figured out how to detect the last_row field hurrah.

killua99’s picture

Status: Active » Needs review

If you get two RTBC I'll commit this patch.

Cause I don't have a live site to test it, or test site.

I need time to create an test env to this and this will take time (because I don't have that time right now).

johnlaine’s picture

#4 did not work for me.

If I check the box "exclude from last row" then I get a completely blank document. The PDF template shows up but none of the field data does. Also there is only a single page, where there would normally be 2 pages.

If I do NOT check the box, I get an extra blank page at the end.

killua99’s picture

Status: Needs review » Needs work

So this patch doesn't work.

I'll take a look this weekend. Can you review the code, and try to submit other patch?

efpapado’s picture

Status: Needs work » Needs review
FileSize
877 bytes

I created another patch. Please note that this is my first patch contribution so I'm not sure everything is right about naming conventions etc.

mikran’s picture

+++ views_pdf_handler_page_break.inc	2014-05-13 07:33:28.000000000 +0300
@@ -68,9 +68,16 @@ class views_pdf_handler_page_break exten
+      else {
+        return '<br pagebreak="true" />';
+      }

This else condition shouldn't be here as it completely prevents 'every_nth' rule from being checked.

I've fixed that and added some code cleanups.

vegansupreme’s picture

#9 works for me. How about one more for RTBC??

TBarina’s picture

#9 works for me, as well.

vegansupreme’s picture

Status: Needs review » Reviewed & tested by the community

  • vegansupreme committed dc82ec8 on 7.x-1.x authored by mikran
    Issue #2194963 by jaimekristene, mikran, efpapado | webdevgirl: Fixed...
vegansupreme’s picture

Status: Reviewed & tested by the community » Closed (fixed)

Committed to dev!

vegansupreme’s picture

Status: Closed (fixed) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.