【问题标题】:printing an iframe with data created from code not is not working使用从代码创建的数据打印 iframe 不起作用
【发布时间】:2018-01-05 17:20:14
【问题描述】:

我有一些示例代码位于 pdfkit 的分支上:

https://github.com/EricG-Personal/pdf_printing/tree/feature/from_pdfkit

我使用 pdfkit 创建简单 PDF 的代码是:

var doc = new PDFDocument( { size: 'legal' } );

this.stream = doc.pipe( blobStream() );

doc.fontSize( 9 );
doc.font( 'Times-Roman' );
doc.text( "hello, world! I'm really here" );
doc.rect( 10, 10, 100, 100 ).stroke();
doc.end();

this.stream.on( 'finish', function() 
{
    console.log( "Stream Finished" );

    this.setState( { pdfData: this.stream.toBlobURL( 'application/pdf' ) } );
}.bind( this ) );

对于 jsPDF - https://github.com/EricG-Personal/pdf_printing/tree/feature/from_jspdf

var doc = new jsPDF({unit: 'pt', format: 'legal'});
var someText = "hello, world!";
var topCoordinate = 72;
var leftCoordinate = 72;
var padding = 8;

doc.setFont( "helvetica" );
doc.setFontSize( 24 );

var lineHeight      = doc.getLineHeight();
var textWidth       = doc.getTextWidth( someText );
var rectHeight      = ( lineHeight + ( padding * 2 ) );
var halfRectHeight  = rectHeight / 2;
var halfLineHeight  = lineHeight / 2;
var textYCoordinate = topCoordinate + halfRectHeight + halfLineHeight;

console.log( "Height: " + lineHeight );
console.log( "Width: " + textWidth );

doc.setDrawColor( 255, 0, 0 );
doc.rect( leftCoordinate, topCoordinate, textWidth, rectHeight );
doc.text( someText, leftCoordinate + padding, textYCoordinate );

doc.setDrawColor( 0, 0, 0 );
doc.rect( leftCoordinate, textYCoordinate - lineHeight, textWidth, lineHeight );

var blob   = doc.output( 'bloburl' );
var mythis = this;

setTimeout( function() 
{ 
    console.log( "Setting State" );

    mythis.setState({pdfData: blob});
}, 5000);

在这两种情况下,PDF 数据都在 iframe 中正确呈现,我最终调用:

window.frames["pdf_doc"].focus();
window.frames["pdf_doc"].print();

当 iframe 的 onLoad 函数触发时。

但是,实际要打印的页面是空白的。

【问题讨论】:

    标签: javascript pdf iframe jspdf node-pdfkit


    【解决方案1】:

    它适用于 Safari,但不适用于 Firefox。我认为您为数组使用了错误的密钥:

    window.frames[0].focus();
    window.frames[0].print();
    

    为我工作。

    console.log(window.frames);
    

    查看文档:https://developer.mozilla.org/de/docs/Web/API/Window/frames

    【讨论】:

    • 我正在使用 Safari。这没用。我使用了正确的密钥。
    • 您确定要打印与我相同的数据吗?
    • 对不起,我使用了来自 github 的代码。你能上传 SRC 吗?
    • github 项目是最新的。令人讨厌的是,您没有看到相同的行为。您是否在最新版本的 OS X 上使用最新版本的 Safari?哦,你说得对,帧的索引应该是一个整数值。与框架 ID 对应的字符串索引不适用于所有浏览器。适用于野生动物园。
    猜你喜欢
    • 1970-01-01
    • 2013-10-01
    • 2015-09-11
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多