How to convert from string into pdf?

前端 未结 3 504
暗喜
暗喜 2021-01-05 15:22

Currently I\'m using Restful service in asp.net c# and the following is the pdf string return that I get, I would like to convert it and save i

相关标签:
3条回答
  • 2021-01-05 15:43
    using (Stream stream = ... fetch the stream from somewhere)
    {
        byte[] buffer = new byte[stream.Length];
        stream.Read(buffer, 0, buffer.Length);
        File.WriteAllBytes("foo.pdf", buffer);
    }
    

    and if this RESTful service talks HTTP you could use a WebClient:

    using (var client = new WebClient())
    {
        client.DownloadFile("http://example.com/api", "foo.pdf");
    }
    
    0 讨论(0)
  • 2021-01-05 15:46

    I had this situation and I solved using iTextSharp to generate the pdf and to save in disk.

    public void GeneratePdf(string htmlPdf)
    {
        var pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        var htmlparser = new HTMLWorker(pdfDoc);
        using (var memoryStream = new MemoryStream())
        {
            var writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
            pdfDoc.Open();
    
            htmlparser.Parse(new StringReader(htmlPdf));
            pdfDoc.Close();
    
            byte[] bytes = memoryStream.ToArray();
            File.WriteAllBytes(@"C:\file.pdf", bytes);
    
            memoryStream.Close();
        }
    }
    
    0 讨论(0)
  • 2021-01-05 15:59

    Sharppdf ? http://sourceforge.net/projects/sharppdf/

    0 讨论(0)
提交回复
热议问题