I want to save canvas as pdf .画布另存为 pdf。
I can get download option using given code, but I did not get proper way to save pdf on specific path.
Can anyone help me to save canvas as pdf on specific path?
Here is my javascript code.
function saveAsPdf() {
var canvas = new fabric.Canvas('c', { selection: false});
var customerId = getParameterByName("intCustomerId");
var image = canvas.toDataURL();
image = image.replace('data:image/png;base64,', '')
var imgData = canvas.toDataURL('image/png');
var doc = new jsPDF('p', 'mm');
doc.save('sample-file.pdf');
}
I found that directly we can't save pdf to specific path.So I used another way to save it.
string tempFileName = "temp.png";
tempPathToSave = HttpContext.Current.Server.MapPath(tempFileName);
using (FileStream fs = new FileStream(tempPathToSave, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
fs.Close();
}
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());
using (XImage img = XImage.FromFile(tempPathToSave))
{
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
xgr.DrawImage(img, 0, 0);
doc.Save(pathToSave);
doc.Close();
}
if (File.Exists(tempPathToSave))
File.Delete(tempPathToSave);
Simply, saved canvas to temparary image and converted image into PDF. I used PDFSharp.dll for this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.