[英]save canvas as pdf on specific path
我想在特定路径上将画布另存为 pdf。
我可以使用给定的代码获得下载选项,但我没有找到在特定路径上保存 pdf 的正确方法。
谁能帮我在特定路径上将画布另存为 pdf?
这是我的 javascript 代码。
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');
}
我发现直接我们不能将pdf保存到特定路径,所以我使用了另一种方式来保存它。
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);
简单地,将画布保存到临时图像并将图像转换为 PDF。 我为此使用了 PDFSharp.dll。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.