2013-07-05 2 views
2

출력 스트림에서 PDF를 생성, 저장 및 작성하고 있습니다.문서를 닫을 때 '문서에 페이지가 없습니다'오류가 발생했습니다.

출력 스트림에 쓰기 중 The document has no page 오류가 발생합니다.

무엇이 문제입니까?

string contents; 
string fileName = "aaa.pdf"; 
Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", "attachment; filename=" + fileName); 
Response.Cache.SetCacheability(HttpCacheability.NoCache); 
StringWriter sw = new StringWriter(); 
HtmlTextWriter hw = new HtmlTextWriter(sw); 
hw.AddStyleAttribute("font-size","11px"); 
abs.RenderControl(hw); 
string path = Server.MapPath("../Images/a.png"); 
contents = sw.ToString(); 
contents = contents.Replace("../Images/a.png", path); 
sw.Close(); 
hw.Close();  
StringReader sr = new StringReader(contents); 

System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/pdf/") + fileName, FileMode.Create); 
Document pdfDoc = new Document(PageSize.A4, 30,5,35,5); 
Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5); 

pdfDoc.PageCount = 2; 
cpdfDoc.PageCount = 2; 
HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
PdfWriter.GetInstance(pdfDoc,fs); 
pdfDoc.Open(); 
htmlparser.Parse(sr); 
pdfDoc.Close(); 
fs.Close(); 
HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc);    
PdfWriter.GetInstance(cpdfDoc, Response.OutputStream); 
cpdfDoc.Open(); 
htmlparser2.Parse(sr); 

cpdfDoc.Close();  

Response.Write(cpdfDoc); 
Response.Flush(); 
Response.End(); 

저장시 문제가 없습니다. 이 줄에 오류가 발생합니다 cpdfDoc.Close();.

+0

를 사용하지 마십시오. 아마도 HTML에서 파싱 할 수있는 것은 아무것도 없었을 것입니다. –

+0

그러나 저장된 pdf에 데이터가 있습니다. – balaji

+0

하지만 콘텐츠 스트림이 비어 있습니다. –

답변

1

이 코드를 사용하고 pdf 위치를 설정하십시오. 및 이미지 경로. 그 작업.

string contents = "hi"; 
     string fileName = "aaa.pdf"; 
     Response.ContentType = "application/pdf"; 
     Response.AddHeader("content-disposition", "attachment; filename=" + fileName); 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter hw = new HtmlTextWriter(sw); 
     hw.AddStyleAttribute("font-size", "11px"); 

     string path = Server.MapPath("~/Images/a.png"); 

     contents = contents.Replace("~/Images/a.png", path); 
     sw.Close(); 
     hw.Close(); 
     StringReader sr = new StringReader(contents); 

     System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/") + DateTime.Now.Ticks, FileMode.Create); 
     Document pdfDoc = new Document(PageSize.A4, 30, 5, 35, 5); 
     Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5); 

     pdfDoc.PageCount = 2; 
     cpdfDoc.PageCount = 2; 
     HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
     PdfWriter.GetInstance(pdfDoc, fs); 
     pdfDoc.Open(); 
     htmlparser.Parse(sr); 
     pdfDoc.Close(); 
     fs.Close(); 
     HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc); 
     PdfWriter.GetInstance(cpdfDoc, Response.OutputStream); 
     cpdfDoc.Open(); 
     htmlparser2.Parse(sr); 

는 당신은 아무 내용도이 문서 인스턴스에 추가되지 않은 예외 "문서가 어떤 페이지가 없습니다"얻을이 줄

contents = sw.ToString(); 
+0

HtmlTextWriter의 목적을 제거했습니다. html 컨트롤에서 내용을 가져오고 있습니다. – balaji

+0

'abs'는 무엇입니까? 먼저 해결책을주는 것보다 말해줘. –

+0

'abs'는 pdf에 대한 데이터가 들어있는 div의 ID입니다. – balaji

관련 문제