How do I access root.$pdf from afterRender?
-
I need to merge my TOC conditionally, so I thought I'd do it manually in
afterRender
. But I don't understand when $pdf is created and in which context it is available.This is my TOC merge code from
afterRender
. The TOC report (usPIYj5DZ) does not get access to root.$pdf when I do it this way. I have probably missed something simple, but I don't know what..//------------- TOC ------------------------------------------------------------ if(isTocEnabled) { const toc = await jsreport.render({ template: { shortid: 'usPlYj5DZ', recipe: 'chrome-pdf' }, data: { ...res.data, } }); res.content = await jsreport.pdfUtils.merge(res.content, toc.content); }
-
I just stumbled over the solution in one of the examples. Posting here if anyone needs this.
if(isTocEnabled) { const pdf = await jsreport.pdfUtils.parse(res.content, true) const toc = await jsreport.render({ template: { shortid: 'usPlYj5DZ', recipe: 'chrome-pdf' }, data: { ...req.data, $pdf: pdf } }); res.content = await jsreport.pdfUtils.merge(res.content, toc.content); }
-
Hm, I do wonder if this is the solution..
I now have access to the$pdf.pages
array, but all of theitems
arrays are empty.I'm adding items like this (guid is a key from objects in an #each loop). I also tried to add a static string as key to ensure nothing was wrong with
guid
. Why doesn't this populate the items array?:{{{pdfAddPageItem id=guid}}}
-
The pdf utils need to remove the hidden marks (items, groups) before the
afterRender
is executed. So you need to skip this in order to useitems
in theafterRender
script.See this here in
options.pdfUtils.removeHiddenMarks
.
https://forum.jsreport.net/topic/2456/can-we-keep-the-page-number-upon-merging-the-pdfs-using-pdf-util/4
-
I can confirm that my
pdfAddPageItem
call works.- If I merge the TOC with the "PDF Utils configuration" I get access to all
items
I have added withpdfAddPageItem
. - If I merge the TOC in
afterRender
as per above, I get the pages array, but all items array are empty.
Why does this differ?
What should I do to access the "real" $pdf.pages array?
- If I merge the TOC with the "PDF Utils configuration" I get access to all
-
Thanks Jan.
This saved my day. I would soon have gone crazy otherwise =)Should I make some extra call lastly in afterRender to make the final cleanup of the hidden marks?
-
Is there any way to get hold of the vertical position of the first TOC row? Sometimes I don't want a pagebreak just before the TOC, so there will be some other elements above the TOC on the same page.
Currently I'm adding a number of pagebreaks before the TOC to render the page numbers on the same page as the TOC is on. It would be even greater if I also could adjust the vertical position.
-
I'll post the vertical-offset question separately to keep the Forum tidy.
-
I'll answer my last question here as well, so the topic gets completely solved.
"Should I make some extra call lastly in afterRender to make the final cleanup of the hidden marks?"
- This can be done by calling
pdfUtils.postprocess
as per the docs (https://jsreport.net/learn/pdf-utils#-postprocess-sourcepdfbuf-pdfmeta-pdfpassword-pdfsign-outlines-)
- This can be done by calling