Printing the Gantt without using the export to pdf

I have had so many issues with the the exportToPDF. It was useful, but lacked a lot of ability in terms of what my company required of me for a printout. After months of trying to figure out how to get it to work, I started to work on a custom solution. I figured out I would share what I did and how it helped.

Basic Idea: Create a copy of the Gantt that allows for possibility to page break and use the browser`s print options for customizability.

HTML(Put this underneath the Gantt):

JS(I am utilizing jquery but the same can be accomplished in vanilla JS):

var PrintStartDate, PrintEndDate, PrintAutosize
function TruePrint(){
PrintStartDate = gantt.config.start_date;
PrintEndDate = gantt.config.end_date;
PrintAutosize = gantt.config.autosize;
gantt.config.autosize = “xy”;
gantt.render()
let CurrGantt = $(#gantt_here).html()
$(.AsOfInsert).text($(#AsOfDate).val())
$(#LoadPrint).hide()
$(.LoadInnerPrint).empty()
let nt = <table> <thead>
nt += <tr style="z-index:10000001"><th>
nt += $(.gantt_grid_scale)[0].outerHTML
nt += </th><th>
nt += $(.gantt_task_scale)[0].outerHTML
nt += </th></tr>
nt += </thead><tbody>
let gr = $(.gantt_row)
let gtr = $(.gantt_task_row)
$(.gantt_task_row).each(function(i,e){
nt += <tr>
nt += <td>
nt += gr[i].outerHTML
nt += </td>
nt += <td class="AddBar" style="position: relative;">
nt += $(this)[0].outerHTML
nt += </td>
nt += </tr>
})
nt += </tbody><tfoot><td></td><td class="addLinks"></td></tfoot></table>

    $(`.LoadInnerPrint`).append(nt)
    $(`.gantt_task_line`).each(function(i,e){
        let elm = $(e)
        elm.css({`z-index`:`1000`,`top`:`17px`})
        $($(`#LoadPrint`).find(`.AddBar`)[i]).append(e)
    })
    $(`#LoadPrint`).find(`.gantt_grid_column_resize_wrap`).remove()
    $(`#LoadPrint`).find(`.gantt_task_cell`).css({`position`:``})
    $(`#LoadPrint`).find(`.gantt_row,.gantt_task_row`).css({`position`:``,`line-height`:``,`left`:``,`top`:``})
    $(`.gantt_task_link:not(.gantt_bar_task)`).each(function(i,e){
        let SID = $(e).attr("class").split(` `)[1].split(`_`)[1]
        let elm = $(e).find(`.gantt_link_arrow, .gantt_line_wrapper`).css({`z-index`:`1000`})
        let init = 0
        $(elm).each(function(i,e){
            if(i == 0){
                init = $(e).css(`top`).split(`p`)[0];
            }
            $(e).css({`top`:(($(e).css(`top`).split(`p`)[0] - init)+17)+`px`})
        })
        $(e).insertAfter($(`#LoadPrint`).find(`.gantt_task_line[data-task-id=`+SID+`]`))
    })
 
}


addEventListener("beforeprint", (event) => {TruePrint()});
addEventListener("afterprint", (event) => {
    gantt.config.start_date =PrintStartDate ;
    gantt.config.end_date =PrintEndDate ;
    gantt.config.autosize =PrintAutosize ;
    $(`#gantt_here`).css({`width`:`100%`, `height`:`80vh`});
        gantt.init("gantt_here");
        gantt.parse(gantt.serialize())
});

CSS:
@media print {
.gantt_row ,.gantt_task_row {
page-break-inside: avoid;
}
body *:not(#LoadPrint, #LoadPrint *){
display: none;
}
#LoadPrint{
display:flex !important;
}
}

Explanation of what I am doing:

  1. Set the autosize to XY. This cause all rows and columns to display.
  2. Create a table in JS with two TDs. On to hold the grid half of the gantt and the other for the chart.
  3. Add in the Bars/Links with the corresponding TDs and change positioning to display in correct formation.
  4. Run function before print.
  5. In CSS, hide everything that is not the table gantt you just made. With the table format, you can put in page-break-inside: avoid; to stop the table from being cut in half on a row during print.

Hello Christian,
Thank you for sharing the details.
I tried to do a similar thing in the snippet, but it doesn’t help (maybe because I didn’t add all the styles):
https://snippet.dhtmlx.com/97xpacbv

However, the idea of using the tables for the task rows in the exported file looks promising. I will forward that information to the dev team. Maybe they will find out how it can help them to implement the multipage export in one file and make sure that task rows are not cut.

Sorry for the confusion. What I was trying to accomplish was to not use the Export Api. With the table method, you can print directly from the browser. The link is to a video demonstrating this functionality.

Hello Christian,
Thank you for the video.
It is clear that you don’t want to use the Gantt’s export function.
But the issue that the tasks are cut in the middle for the multipage export is still present in the Gantt’s export module. The approach you suggested may help the developers to fix that issue.