tja, det här är ett sätt:
protected void DoCreatePDF(object sender, CommandEventArgs e)
{
// Create url of print template and pass the key for cache
string templatePath = String.Format("http://{0}{1}?rt=1&ft={3}&t={4}&hi={5}&guid={2}", Request.Url.Host, VirtualPathUtility.ToAbsolute("~/Templates/Report.aspx"), id, tbPerArrivalFrom.Text + '-' + tbPerArrivalTo.Text, DateTime.Now.Ticks, Page.Hotel.HotelId);
var startPageFullUrl = String.Format("http://{0}{1}?ts={2}", Request.Url.Host, VirtualPathUtility.ToAbsolute("~/Templates/ReportFirstPage.aspx"), DateTime.Now.Ticks);
// Create PDF from template
var manager = new PDFManager();
byte[] pdf = manager.GeneratePDF(templatePath, startPageFullUrl, PDFManager.PdfRotation.Landscape, PDFManager.SourceDocumentType.HTML, true);
// Create filename
string fileName = String.Format("Report_{0}-{1}.pdf", DateTime.Now.Date.ToShortDateString(), DateTime.Now.ToShortTimeString());
// Clear the response, add file and change the content type
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Clear();
Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", fileName));
Response.BinaryWrite(pdf);
Response.End();
}
och generatepdf ungefär dåhär... inte så vacker kanske men jag orkar inte mecka om den. Denna har ju som du ser lite andra saker, som försättsblad och sidhuvud sidfot etc, men du kan ju utgå från den, copy paste funkar nog inte rakt av :)
public byte[] GeneratePDF(string sourceDocumentFullUrl, string startPageFullUrl, PdfRotation pdfRotation, SourceDocumentType sourceDocumentType, bool addStartPage)
{
Doc mainDocument = new Doc();
int pageID;
mainDocument.HtmlOptions.DoMarkup = true;
mainDocument.HtmlOptions.PageCacheEnabled = false;
if (addStartPage){
//// Set text to first page
string firstPageText = string.Format("Rapport {0}", DateTime.Now.ToShortDateString());
mainDocument.Font = mainDocument.AddFont("Verdana");
mainDocument.HPos = 0.5;
mainDocument.VPos = 0.75;
mainDocument.AddHtml(firstPageText);
mainDocument.AddImageUrl(startPageFullUrl);
}
pageID = mainDocument.AddPage();
if (pdfRotation == PdfRotation.Landscape)
RotateDocument(mainDocument);
mainDocument.Page = pageID;
pageID = mainDocument.AddImageUrl(sourceDocumentFullUrl);
while (true){
if (!mainDocument.Chainable(pageID))
break;
mainDocument.Page = mainDocument.AddPage();
pageID = mainDocument.AddImageToChain(pageID);
}
for (int i = 1; i <= mainDocument.PageCount; i++){
mainDocument.PageNumber = i;
mainDocument.Flatten();
if (i > 1){
// Footer
mainDocument.Rect.Width = 600;
mainDocument.Rect.Height = 30;
mainDocument.Rect.Inset(0, 0);
mainDocument.Rect.Position(0, 0.3);
mainDocument.HPos = 0.5;
mainDocument.VPos = 0.5;
mainDocument.AddText(string.Format("Report Yieldmanager {0} / {1}", i - 1, mainDocument.PageCount - 1));
}
}
byte[] generatedPDF = mainDocument.GetData();
mainDocument.Clear();
mainDocument.Dispose();
return generatedPDF;
}
det du frågar efter är EGENTLIGEN bara det sista i första metoden...
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Clear();
Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", fileName));
Response.BinaryWrite(pdf);
Response.End();
lycka till