描述:我有一個網站,通過iFrame動態加載PDF。 PDF保存在服務器上,網站用戶可以在網站上查看PDF。
問題:在網站上引入一個打印按鈕,打印動態創建並保存在服務器上的PDF。
解決方案:我找不到這個問題的精確解,但這裏是我是如何解決這個問題 -
- 創建「打印」按REQ和重定向到具有唯一的另一頁PDF。
- 複製上一頁PDF &使用JS創建新的PDF - this.print(),當打開時,打印對話框直接彈出給用戶。
在新的頁面 -
if ("Location of PDF " != null)
{
sPdf = "Location of PDF ";
PdfReader pReader = new PdfReader(sPdf);
Document document = new Document
(pReader.GetPageSizeWithRotation(ApplicationConstants.INDEX_ONE));
int n = pReader.NumberOfPages;
FileStream fs = new FileStream
("New PDF location",
FileMode.Create, FileAccess.Write);
PdfCopy copy = new PdfCopy(document, fs);
// Write to pdf
document.Open();
for (int i = ApplicationConstants.INDEX_ONE; i <= n; i++)
{
PdfImportedPage page = copy.GetImportedPage(pReader, i);
copy.AddPage(page);
}
copy.AddJavaScript("this.print(true);", true);
document.Close();
pReader.Close();
inStr = File.OpenRead("New PDF location");
while ((bytecnt = inStr.Read
(buffer, ApplicationConstants.INDEX_ZERO, buffer.Length))
> ApplicationConstants.INDEX_ZERO)
{
if (Context.Response.IsClientConnected)
{
Context.Response.ContentType = "application/PDF";
Context.Response.OutputStream.Write(buffer,
ApplicationConstants.INDEX_ZERO, buffer.Length);
Context.Response.Flush();
}
}
}
請注意,我使用iTextSharp的注入JS腳本到新的PDF。希望這可以幫助別人。我試圖找到另一種解決方案,而不使用itextsharp或任何其他DLL,但現在必須做。
如果您已將pdf保存在服務器上,並且看起來像您想要的那樣,您可以提供一個定期鏈接,它將使用戶閱讀pdf。從那裏打印它會很容易。然而,如果你希望「點擊一下並從打印機出來」的解決方案,壞消息是沒有一個。相關:http://stackoverflow.com/questions/5627035/how-to-print-the-contents-of-an-external-file-in-php – lostphilosopher
我假設你已經確定這(http: //www.javascriptkit.com/howto/newtech2.shtml)不適合你? – lostphilosopher
>是否有將PDF文檔從服務器傳遞到打印對話框的選項? ......當然不是,只要你希望其他人能夠盲目地將東西發送到你的機器上並做一些事情。 –