Reputation: 51
I am trying to save imageData as pdf file on server directory. Html5Canvas imageData was sent to server and after conversion in bytes array, tried to save as PDF file. File generated successfully on specified path but the generated file doesn't open correctly in most of the PDF readers(i.e. Adobe Reader, Foxit reader etc) and show error that file is either damaged or corrupt but it open correctly in MS Edge browser. I want them to show in common PDF reader too. Can you please suggest the solution. Here is my server side code.
public static string SaveImage(string imageData, string userEmail, int quantity)
{
string completePath = @"~\user-images\";
string imageName = "sample_file2.pdf";
string fileNameWitPath = completePath + imageName;
byte[] bytes = Convert.FromBase64String(imageData);
File.WriteAllBytes(HttpContext.Current.Server.MapPath(fileNameWitPath), bytes);
}
Same output generated for this code
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(fileNameWitPath), FileMode.OpenOrCreate);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
and for this too.
using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(fileNameWitPath), FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
Upvotes: 0
Views: 3615
Reputation: 2135
If you just save a raster image format file (like PNG or JPG one) with a .PDF
file extension it doesn't make it a PDF file; it still remains an image file just with another extension. So it probably works in some browsers because they may do file format detection that is not based on extension alone.
To generate an actual PDF file you will need to employ some conversion. Consider one of the following libraries for this:
Upvotes: 1