【问题标题】:Facing some design issue about jsPDF面临一些关于 jsPDF 的设计问题
【发布时间】:2015-08-20 16:45:40
【问题描述】:

我正在使用 jsPDF,它工作正常。它将文档下载为“PDF”,但问题是我的设计错误,我希望表格显示在网站上。

jsPDF中如何设置表格的列高和宽?

这是我的脚本

function exportPdf()
{
    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 = $('#infobox')[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
        '#infobox': function (element, renderer) {
            // true = "handled elsewhere, bypass text extraction"
            return true
        }
    };
    margins = {
        top: 10,
        bottom: 10,
        left: 10,
        width: "100%"
    };
    // 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('musterData.pdf');
    }, margins);
    return false;
} 

【问题讨论】:

  • 这里的任何人都可以帮助...

标签: javascript pdf jspdf


【解决方案1】:

我已经发布了与您的问题相关的示例代码。该代码将使用 jsPDF 工具将 html 表导出为 PDF 格式

寻找以下几行

doc.cellInitialize();

。 . .

doc.cell(leftMargin, topMargin, cellWidth, rowWidth, cellContent, i)

以下是找到我所说的示例代码的链接

How to set column width for generating pdf using jsPDF

如果您这样做,您将获得 PDF 格式的表格。如果您有任何问题,请告诉我。如果您觉得有帮助,请不要忘记投票

【讨论】:

  • Hello Vinu 我的表格列不固定,列的大小也不同。我正在使用jsfiddle.net/xzZ7n/18 示例这里我正在获取 pdf,但表格的宽度很短,它不适合 pdf 页面宽度
  • 嗨 Nilesh,抱歉回复晚了。我想知道两件事----首先你是否已经解决了?其次,我查看了您的代码并注意到您正在以“pt”为单位初始化 PDF [即在这一行中 var pdf = new jsPDF('l', 'pt', 'a4');] 和如您的 cmets 中所述,以英寸为单位设置边距?如果这是故意的,请告诉我
  • 未解决,但使用了使用 php 的服务器端 pdf 脚本,但仍然存在此问题。实际上在 jspdf 中根据我的假设“pt,in,cm”这些单位是在初始化开始时分配的“ var pdf = new jsPDF('l', 'pt', 'a4');]" (这里是 l-landscape,pt-unit,a4-page)这就是我使用 pt....我主要关心的是我的桌子水平有 35 列....但是当我在上面的示例中使用 jspdf 时,它只在横向页面上生成 20 到 25 列
  • 好的,尼莱什。请查看以下链接,您可以在其中找到 JSPdf 的类、字段和方法的摘要。它可能对你有用。它说单位是指定坐标时要使用的测量单位。 “pt”(点)、“mm”(默认)、“cm”、“in”之一。
  • 我说的链接是given here。谈到实际问题,我不得不对大约 15 列不同列宽的列使用 JSPdf 库。它要么太小要么超过 PDF 的横向视图,所以我不得不编写通过循环计算列名的长度及其行值宽度并设置最大长度。在某些情况下,我拆分单词以便它显示在同一列的下一行。您也尝试动态计算列宽,这可能会有所帮助
猜你喜欢
  • 1970-01-01
  • 2016-08-25
  • 2011-02-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多