HTML to PDF conversion - Almost works, but - c#

I spent a good part of last few days searching for easy-to-use library that takes a html string as input, and produces PDF output as a file to the client browser.
Of the few dozen tools I tried out, a product called NReco PDF Generator, which is one of many derivative tools based on wkhtmltopdf, seems to suit my needs.
Here's my test code:
var strHtml = String.Format("<h1>Hello World!</h1");
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
(new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(strHtml, null, Response.OutputStream);
Response.End();
With the above code, I expected to see a PDF file being passed to my browser. Instead, I only see the output stream being generated in my developer console. (BTW, I'm using Chrome)
My question is NOT about how the said product works, which I think is working as intended, but it is about whether my code is missing any action for the .NET's Response object or is doing anything wrong. As I already indicated, I can see the converted Pdf stream in the console. I think it's just a matter of capturing that stream in a file and passing it to the client.

The missing line is Response.BinaryWrite(pdfBytes). Without that line the PDF will not be rendered to the user.
As far as I can tell you do not need the lines:
Response.Clear();
Response.ClearHeaders();
The following works for me (Obviously, you need to add using NReco.PdfGenerator;)
string strHtml = String.Format("<h1>Hello World!</h1");
HtmlToPdfConverter pdfConverter = new HtmlToPdfConverter();
var pdfBytes = pdfConverter.GeneratePdf(strHtml);
Response.ContentType = "application/pdf";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "Inline; filename=TEST.pdf");
Response.BinaryWrite(pdfBytes);
Response.Flush();
Response.End();

Store the PDF as a byte array, then write it to the response.
var pdfBytes = new NReco.PdfGenerator.HtmlToPdfConverter().GeneratePdf(strHtml);
Response.BinaryWrite(pdfBytes);
instead of
(new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(strHtml, null, Response.OutputStream);
Inspiration for this taken from NReco's site.

Related

Download file with non-english character in C# using HttpResponse

I am trying to build a functionality to download a csv file in C#.
When the name of the file has non-english character, the downloaded file does not seems to have the correct name. However in the network tab, the response header has the same Content-Disposition value, as given in the code.
Sample Code
private void PopulateCsvInResponse(MemoryStream csvData, string fileName)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
//actual file name "Москва.csv"
response.AddHeader("Content-Disposition", "attachment; filename=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv");
byte[] byteArray = csvData.ToArray();
response.AddHeader("Content-Length", byteArray.Length.ToString());
response.ContentType = "text/csv; charset=utf-8";
response.BinaryWrite(byteArray);
response.Flush();
response.Close();
}
For example the file name is Москва.csv.
UTF-8 encoded name : %D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv.
Things that I tried
Replacing Content-Disposition header
Attempt 1
response.AddHeader("Content-Disposition",
"attachment; filename=Москва.csv");
The downloaded file name is
Ð_оÑ_ква
Attempt 2
response.AddHeader("Content-Disposition",
"attachment; filename=\"%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv\"; filename*=UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv");
The downloaded file name is
_%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv_; filename_
Attempt 3
response.AddHeader("Content-Disposition",
"attachment; filename=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv");
The downloaded file name is
%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv
Attempt 4
response.AddHeader("Content-Disposition",
"attachment; filename*=UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv");
The downloaded file name is
UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv
I finally found out the solution.
The issue was never with above code, it was always working fine.
The actual issue was in front end, where the content disposition header that was received in encode was not decoded, and I skipped to see this part when I raised the question.
I thought of deleting this question, but keeping it so that if someone makes the same silly mistake as me, might realise it earlier instead of wasting time to look for solution for the problem that never existed.

.net CSV file stream to browser loosing characters on production server

I've got basic functionality to stream a file to the browser which invokes a "Save As". The output is dynamically generated and stored within a string and not a file saved on the server.
See code below.
string output = GenerateCSVDdata;
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=\"test.csv\");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
Response.Write(output);
Response.End();
Now, on my development server, the CSV fully downloads. On the production server, the last few characters on end are cut off. The larger the CSV, the more characters are missing. I've tried so many different things like Response.Flush etc but nothing can fix it. The only thing I can do is throw a load of empty chars on the end in hope nothing gets cut.
Is there something quite wrong with this method of streaming a file download without actually saving the file to disk?
Thanks for your help.
Can you determine if there is a difference in the byte count for the .csv file you are using?
byte[] defaultEncodingBytes = System.Text.Encoding.Default.GetBytes(defaultEncodingFileContents);
byte[] UTF8EncodingBytes = System.Text.Encoding.UTF8.GetBytes(defaultEncodingFileContents);
Try this, it worked for me.
void DownloadFile(string filename)
{
//string filename = "c:\\temp\\test.csv";
byte[] contents = System.IO.File.ReadAllBytes(filename);
Response.Clear();
Response.ClearHeaders();
Response.AppendHeader("Content-disposition", String.Format("attachment; filename=\"{0}\"", System.IO.Path.GetFileName(filename)));
Response.AppendHeader("Content-Type", "binary/octet-stream");
Response.AppendHeader("Content-length", contents.Length.ToString());
Response.BinaryWrite(contents);
if (Response.IsClientConnected)
Response.Flush();
}
Regards.

Download Document in the right format without MIME

I have got this WebService that allows uploading/downloading any docs (mostly .docx, .doc, .pdf) and all it returns is byte[] when querying for downloading.
I have written this code
string ContractGUID = dtContract.Rows[0]["ContractGUID"].ToString();
//Get Bytes from WebService
byte[] fileData = BLL.Contract.GetDocument(new Guid(ContractGUID));
Response.Clear();
Response.BinaryWrite(fileData);
Response.AddHeader("Content-Disposition", "Attachment");
Response.Flush();
The other methods that the WebService exposed are GetDocumentName and GetDocumentLen
Is it possible to determine the Mime-Type or force the browser to download it in the right format? Currently it is downloading as .htm in Chrome and when open, I see funny characters. Any better advice?
Thanks.
No, it's not possible to force the browser to download in the right format without you telling it via the Content-Type header.
Response.ContentType = "application/pdf"; //or whatever appropriate
If the web service exposes a GetDocumentName() method you can probably infer the appropriate format by looking at the name, assuming the name has a file extension. This, obviously, is not bullet proof since you can change the extension of a file to anything you want.
Another alternative would be to try and guess the file format by peeking at the first bytes. For example, if the first 4 bytes of the file are 25 50 44 46 then it's very likely that this is a PDF file. On this website, they have a pretty extensive list.
Here's the list of possible content-type headers.
I think, the browser does it through filename.
e.g.:
response.Clear();
response.AddHeader("Content-Disposition", "attachment; filename=" + dbFile.filename.Replace(" ", "_"));
response.AddHeader("Content-Length", dbFile.data.Length.ToString());
response.ContentType = "application/octet-stream";
response.OutputStream.Write(dbFile.data, 0, dbFile.data.Length);
response.End();
dbFile.filename is a string
dbFile.data is a byte[]

iTextSharp generated PDF: How to send the pdf to the client and add a prompt?

I have generated a pdf using iTextSharp, when its created it saves automatically in the location provided in my code on the server not on the client side and of course without telling anything to the user.
I need to send it to the client and I need to prompt a dialogue box to ask the user where he wants to save his pdf..
how can i do this please?
this is my pdf code:
using (MemoryStream myMemoryStream = new MemoryStream())
{
Document document = new Document();
PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);
document.AddHeader("header1", "HEADER1");
document.Open();
//..........
document.Close();
byte[] content = myMemoryStream.ToArray();
// Write out PDF from memory stream.
using (FileStream fs = File.Create(HttpContext.Current.Server.MapPath("~\\report.pdf")))
{
fs.Write(content, 0, (int)content.Length);
}
EDIT
this is an example of the result i want
http://examples.extjs.eu/?ex=download
thanks to your replies ,I modified my code to this:
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AppendHeader( "Content-Disposition", "attachment; filename=test.pdf");
using (MemoryStream myMemoryStream = new MemoryStream())
{
Document document = new Document();
PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);
document.AddHeader("Content-Disposition", "attachment; filename=wissalReport.pdf");
document.Open();
//..........
document.Close();
byte[] content = myMemoryStream.ToArray();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("content-disposition","attachment;filename=" + "my_report.pdf");
HttpContext.Current.Response.ContentType = "Application/pdf";
//Write the file content directly to the HTTP content output stream.
HttpContext.Current.Response.BinaryWrite(content);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
but i get this error:
Uncaught Ext.Error: You're trying to decode an invalid JSON String:
%PDF-1.4 %���� 3 0 obj <</Type/XObject/Subtype/Image/Width 994/Height 185/Length 13339/ColorSpace/DeviceGray/BitsPerComponent 8/Filter/FlateDecode>>stream x���|E�
...........
im absolutely sure my itextsharp to create pdf is correct because i can save it on the server, but thats not what i need to do ,when i try to send it to the client i got the error above
thanks in advance
In case of a web application you probably want to stream the pdf as binary to user, that would either open the pdf or prompt user to save the file.
Remember pdf generation is happening at server, even if user provides the path it won't be of any use on server. See following links -
How To Write Binary Files to the Browser Using ASP.NET and Visual C# .NET
In your case you are generating the file and hence will already be having a binary stream instead of file, hence you can directly use Response.BinaryWrite instead of Response.WriteFile.
Modified sample:
Response.Buffer = false;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Write the file content directly to the HTTP content output stream.
Response.BinaryWrite(content);
Response.Flush();
Response.End();
You need to send a content disposition header to the users browser. From memory the code is something sort of like this:
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition","attachment; filename=nameofthefile.pdf");
Currently you are saving your file on the file server, thereby overwriting the same pdf with every request. And probably causing errors if you get two requests for a PDF at the same time.
Use Response to return the PDF (from the memorystream) to the user, and skip the writing of the PDF to a file locally on your server.
The browser will ask the user where the file should be saved. Something like:
Response.ContentType = "Application/pdf";
myMemoryStream.CopyTo(Response.OutputStream);
Also look at the answer from Alun, using content-disposition you can propose a filename to the user.
SOLVED
The error is from the submit operation trying to interpret the response which it can not because it is not in a known format.
I just set window.location to download files and this works fine.
{
xtype:'button',
text: 'Generate PDF',
handler: function () {
window.location = '/AddData.ashx?action=pdf';
}
}
Instead of setting the location you can also do window.open().
Whether the file will be downloaded or opened depends on browser settings.
You do not need to use MemoryStream. Use Response.OutputStream instead. That's what it's there for. No need to use Response.BinaryWrite() or any other call to explicitly write the document either; iTextSharp takes care of writing to the stream when you use Response.OutputStream.
Here's a simple working example:
Response.ContentType = "application/pdf";
Response.AppendHeader(
"Content-Disposition",
"attachment; filename=test.pdf"
);
using (Document document = new Document()) {
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
document.Add(new Paragraph("This is a paragraph"));
}
Here's how to add the proper HTTP headers. (getting the prompt to save the file) And if your code is in a web form, (button click handler), add Response.End() to the code example above after the using statement so that the web form's HTML output is not appended the PDF document.

Exported CSV download stuck in firefox

I'm exporting a CSV to firefox using the following code:
string csv = dataTable.ToCSV();
Response.ClearContent();
Response.AddHeader("Content-disposition", "attachment;filename=solicitud.csv");
Response.AddHeader("Content-length", (Encoding.Unicode.GetBytes(csv).Length).ToString());
Response.ContentType = "application/excel";
Response.ContentEncoding = Encoding.Unicode;
Response.Write(csv);
Response.End();
However, firefox get stucked in "starting" when downloading the files, my guess is that firefox keeps waiting to receive more bytes, this only happens in firefox, IE works fine, Am I missing a header or do you see anything wrong with the code?
Instead use TransmitFile
Writes the specified file directly to
an HTTP response output stream without
buffering it in memory.

Categories