简体   繁体   English

在特定路径上将画布另存为 pdf

[英]save canvas as pdf on specific path

I want to save canvas as pdf on specific path .我想在特定路径上将画布另存为 pdf。
I can get download option using given code, but I did not get proper way to save pdf on specific path.我可以使用给定的代码获得下载选项,但我没有找到在特定路径上保存 pdf 的正确方法。
Can anyone help me to save canvas as pdf on specific path?谁能帮我在特定路径上将画布另存为 pdf?
Here is my javascript code.这是我的 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');
}

I found that directly we can't save pdf to specific path.So I used another way to save it.我发现直接我们不能将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);

Simply, saved canvas to temparary image and converted image into PDF.简单地,将画布保存到临时图像并将图像转换为 PDF。 I used PDFSharp.dll for this.我为此使用了 PDFSharp.dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM