下载图表和表格的PDF格式

Download graph as well as table as a PDF

本文关键字:PDF 格式 表格 下载      更新时间:2023-09-26

我有一个D3图表和一个引导表。我想下载图形和表,因为它看起来在HTMl页面上。我找到了一些答案,但他们要么下载表格,要么下载图表。我想要一个相同的组合PDF。我试过用jspdf,但我卡住了。

下面是jsfiddle的链接。

http://jsfiddle.net/xzZ7n/3301/

function demoFromHTML() {
    var pdf = new jsPDF('p', 'pt', 'letter');
    // source can be HTML-formatted string, or a reference
    // to an actual DOM element from which the text will be scraped.
    source = $('#customers')[0];
    // we support special element handlers. Register them with jQuery-style 
    // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
    // There is no support for any other type of selectors 
    // (class, of compound) at this time.
    specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        '#bypassme': function (element, renderer) {
            // true = "handled elsewhere, bypass text extraction"
            return true
        }
    };
    margins = {
        top: 80,
        bottom: 60,
        left: 40,
        width: 522
    };
    // all coords and widths are in jsPDF instance's declared units
    // 'inches' in this case
    pdf.fromHTML(
    source, // HTML string or DOM elem ref.
    margins.left, // x coord
    margins.top, { // y coord
        'width': margins.width, // max width of content on PDF
        'elementHandlers': specialElementHandlers
    },
    function (dispose) {
        // dispose: object with X, Y of the last line add to the PDF 
        //          this allow the insertion of new lines after html
        pdf.save('Test.pdf');
    }, margins);
}

后端是spring MVC。JQuery或JAVA的下载功能都可以。

因此,出于好奇和考虑,我不完全确定jsPDF是否最可靠。首先,使用d3.js将图形呈现为SVG,因此最好的选择是将SVG转换为PDF。我发现了这个问题,似乎fromHTML仍然是一个非常新的方法(它甚至没有在内部文档中…)。

他们确实提到他们支持addSVG方法,但我发现这也可能不像你想的那样理想。听起来它仍然有一些问题,你并不是唯一一个问这个库的SVG。

看了看之后,我认为这个答案总结得最好——签出PDFKit,它给了你一种将SVG渲染成PDF的方法,尽管在看了文档之后,似乎它们只允许你画画?它没有展示向PDF文档添加SVG的方法。答案还提到了一个svgToPDF插件的jsPDF,但我看了看,显然这是合并了一段时间前,所以我不确定如果它实际上是一个插件或已经内置。

希望这能提供更多的见解。我自己也对这个问题很好奇。如果我找到更好的信息,我会更新这个答案。