如何在iframe中打印pdf?

时间:2022-10-30 23:40:48

At cbjsonline.com, I'm trying to have a pdf in an iframe print automatically with javascript.

在cbjsonline.com,我正在尝试使用javascript自动在iframe中打印pdf。

Currently, my code is - (connected to the onclick of the link that opens the iframe) - document.getElementById('fancy_frame').onload = setTimeout('window.print()',2500);

目前,我的代码是 - (连接到打开iframe的链接的onclick) - document.getElementById('fancy_frame')。onload = setTimeout('window.print()',2500);

Any suggestions? This method only works in safari.

有什么建议?此方法仅适用于safari。

2 个解决方案

#1


Try passing a function pointer to setTimeout, instead of an expression that gets eval'd.

尝试将函数指针传递给setTimeout,而不是获得eval'd的表达式。

document.getElementById('fancy_frame').onload = setTimeout( printWindow, 2500 );

// implemented in the HTML that is loaded in 'fancy_frame'
function printWindow()
{
    window.print();
}

#2


OK, I decided to let the user print the page. I think iFrames with PDFs are too variable trying to print with javascript. If anyone wants to try this, this is what I'd recommend. Use the jquery load, which checks for assets, not just loading, not the onload handeler, because the delay for adobe reader varies greatly across computers. Also, try naming and focusing the iframe before printing (by name, so iframe.print(), instead of window.print()), that way it won't try to print the page your currently on. It might be a better idea just to use the scribd ipaper viewer for this application.

好的,我决定让用户打印页面。我认为带有PDF的iFrames变化太大,试图用javascript打印。如果有人想尝试这个,这就是我推荐的。使用jquery load来检查资产,而不仅仅是加载,而不是onload handeler,因为adobe reader的延迟在计算机之间变化很大。另外,在打印之前尝试命名和聚焦iframe(通过名称,iframe.print(),而不是window.print()),这样就不会尝试打印当前页面。将scribd ipaper viewer用于此应用程序可能是一个更好的主意。

#1


Try passing a function pointer to setTimeout, instead of an expression that gets eval'd.

尝试将函数指针传递给setTimeout,而不是获得eval'd的表达式。

document.getElementById('fancy_frame').onload = setTimeout( printWindow, 2500 );

// implemented in the HTML that is loaded in 'fancy_frame'
function printWindow()
{
    window.print();
}

#2


OK, I decided to let the user print the page. I think iFrames with PDFs are too variable trying to print with javascript. If anyone wants to try this, this is what I'd recommend. Use the jquery load, which checks for assets, not just loading, not the onload handeler, because the delay for adobe reader varies greatly across computers. Also, try naming and focusing the iframe before printing (by name, so iframe.print(), instead of window.print()), that way it won't try to print the page your currently on. It might be a better idea just to use the scribd ipaper viewer for this application.

好的,我决定让用户打印页面。我认为带有PDF的iFrames变化太大,试图用javascript打印。如果有人想尝试这个,这就是我推荐的。使用jquery load来检查资产,而不仅仅是加载,而不是onload handeler,因为adobe reader的延迟在计算机之间变化很大。另外,在打印之前尝试命名和聚焦iframe(通过名称,iframe.print(),而不是window.print()),这样就不会尝试打印当前页面。将scribd ipaper viewer用于此应用程序可能是一个更好的主意。