2011-12-02 5 views
2

나는 pdf 문서를 만들기 위해 winnovative html to pdf 변환기를 사용하고 있습니다. 사용자가 인쇄하거나 pdf 파일을 전자 메일로 보낼 것인지 묻는 확인란을 추가했습니다.프린터 대화 상자 열기

하지만 프린터 대화 상자를 열려면 pdf 페이지를 가져올 수 없습니다. PrinterDialog 클래스를 시도했지만 작동하지 않아 window.print()가 작동하지 않는 일부 JavaScript를 보냈습니다. 나는 인터넷을 검색했지만 아무것도 찾을 수 없습니다.

Response.Clear(); 
Response.ContentType = "application/pdf"; 
Response.AppendHeader("Content-Disposition", "inline;filename=Offerte.pdf"); 
Response.BufferOutput = true; 
Response.AddHeader("Content-Length", downloadBytes.Length.ToString()); 
Response.BinaryWrite(downloadBytes); //downloadBytes = the byte array created by winnovative converter 
Response.End(); 

이 PDF로 내 페이지가 포함 된 브라우저 내 PDF 뷰어가 열립니다 : PDF를 포함

내 페이지에는 다음과 같은 코드가 있습니다. 여기에서 사용자는 pdf 뷰어/브라우저의 인쇄 버튼을 클릭 할 수 있습니다. 하지만 내 페이지가 프린터 대화 상자를 열거 나 사용자가해야하는 작업을 최소화하기 위해 바이트를 프린터로 직접 보내고 싶습니다.

아이디어가 있으십니까?

+2

Chack이 답변 http://stackoverflow.com/a/2495430/293712을 – Maheep

+0

호출 window.print() 몸의 온로드 작업을해야합니다. 인쇄용 코드를 보여줄 수 있습니까? – mehul9595

+0

인쇄 할 특정 코드가 없습니다. 브라우저에서 프린터 대화 상자를 열면됩니다. 나는 window.print()를 내 머리글에 추가하려고 시도했다. body onload와 변환 전에 html 코드의 본문 내부. 이 모든 것이 효과가 없었습니다. 심지어 CMS의 내 페이지의 페이지 템플릿 헤더에 넣으려고했습니다. 내 CMS 외부에서 실행하려고했습니다. 이 모든 것이 효과가 없었습니다. – Pedryk

답변

0

오늘 아침에 해결했습니다. 단지 어리석은 실수로 보입니다. winnovative 변환기에는 기본적으로 false로 설정된 스크립트를 사용하기위한 매개 변수가 있습니다. 이것을 true로 설정하면 pdf에서 자바 스크립트를 사용할 수있었습니다.

나에게 제안 된 게시물을 읽은 후 PDF에서 자바 스크립트를 사용할 수 있어야한다는 사실을 발견했습니다. winnovative에서 FAQ를 포함하여 좀 더 검색 한 후 다음 코드를 추가했습니다.

pdfConverter.ScriptsEnabled = true; 
pdfConverter.ScriptsEnabledInImage = true; 
pdfConverter.InternetSecurityZone = InternetSecurityZone.LocalMachine; 

그런 다음 헤더 안의 JavaScript가 작동했습니다!

<script type="text/javascript"> 
window.print(); 
</script> 
1

PDF를 스트리밍 중이므로 제한된 옵션이 있습니다.

나는이 접근법을 사용하는 것이 가장 좋은 방법이라고 생각한다 : https://stackoverflow.com/a/2495430/293712. 새 창에서 PDF를 엽니 다 (이 새 창에서이를 스트리밍 할 수 있음). 그런 다음 window.print를 부모 창 (window.open을 사용하여 여는 경우)에서 호출하고 완료되면 창을 닫을 수도 있습니다.

+0

글쎄, 나는 제안을 위해 열려있다. 바이트 []를 프린터로 직접 보내는 방법을 알고 있습니까? – Pedryk

+0

새 창에서 열리는 페이지. 동일한 스트리밍 코드를 사용합니다. 그러나 JS는 인쇄 가능한 페이지가 아닌 상위 페이지의 양식을 실행합니다. 그것은 그걸 해결하기위한 방법입니다 ... –

+0

콘텐츠 유형을 변경했기 때문에 문제 중 하나가 콘텐츠 유형을 PDF로 처리 할 수 ​​없으므로 부모 창은 ... –

0

PDF 문서를 뷰어에서 열 때 실제로 실행될 Acrobat JavaScript 코드를 추가 할 수 있습니다. Open Print Dialog 옵션을 선택하면 Execute Acrobat JavaScript Code when Document is Opened demo에 작업 예제가 표시됩니다. 이 데모에서 관련 C# 코드는 아래에 복사 :

protected void convertToPdfButton_Click(object sender, EventArgs e) 
{ 
    // Create a HTML to PDF converter object with default settings 
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); 

    // Set license key received after purchase to use the converter in licensed mode 
    // Leave it not set to use the converter in demo mode 
    htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og="; 

    Document pdfDocument = null; 
    try 
    { 
     // Convert a HTML page to a PDF document object 
     pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text); 

     string javaScript = null; 
     if (alertMessageRadioButton.Checked) 
     { 
      // JavaScript to display an alert mesage 
      javaScript = String.Format("app.alert(\"{0}\")", alertMessageTextBox.Text); 
     } 
     else if (printDialogRadioButton.Checked) 
     { 
      // JavaScript to open the print dialog 
      javaScript = "print()"; 
     } 
     else if (zoomLevelRadioButton.Checked) 
     { 
      // JavaScript to set an initial zoom level 
      javaScript = String.Format("zoom={0}", int.Parse(zoomLevelTextBox.Text)); 
     } 

     // Set the JavaScript action 
     pdfDocument.OpenAction.Action = new PdfActionJavaScript(javaScript); 

     // Save the PDF document in a memory buffer 
     byte[] outPdfBuffer = pdfDocument.Save(); 

     // Send the PDF as response to browser 

     // Set response content type 
     Response.AddHeader("Content-Type", "application/pdf"); 

     // Instruct the browser to open the PDF file as an attachment or inline 
     Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Execute_Acrobat_JavaScript.pdf; size={0}", outPdfBuffer.Length.ToString())); 

     // Write the PDF document buffer to HTTP response 
     Response.BinaryWrite(outPdfBuffer); 

     // End the HTTP response and stop the current page processing 
     Response.End(); 
    } 
    finally 
    { 
     // Close the PDF document 
     if (pdfDocument != null) 
      pdfDocument.Close(); 
    } 
}