簡體   English   中英

在特定路徑上將畫布另存為 pdf

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM