iT邦幫忙

0

iTextSharp網頁轉PDF問題請教

https://ithelp.ithome.com.tw/upload/images/20190927/20110132x2a0zK2xML.jpg

這是我某個功能,按下修改後會出現一張表單
https://ithelp.ithome.com.tw/upload/images/20190927/20110132tnnVfnZR4I.jpg

我想將這個表單轉成PDF檔
但目前路徑好像有問題,想請各位幫我看一下

這是登入表單後的URL

http://localhost:11477/NIS/Guide/PDPatGuideTestEdit?strGuid=98AA91FA-1187-4FA3-9C02-A5044FB3FB2A

我是參考這個網站的
https://dotblogs.com.tw/shadow/2014/02/09/143891

這是我的controller

 [HttpGet]
        public ActionResult ExportPDF(string strGuid)
        {      
            WebClient wc = new WebClient();
            string htmlText = wc.DownloadString("http://localhost:11477/NIS/Guide/PDPatGuideTestEdit?strGuid"+strGuid);
            byte[] pdfFile = this.ConvertHtmlTextToPDF(htmlText);

            return File(pdfFile, "application/pdf", "範例PDF檔.pdf");
        }

        private byte[] ConvertHtmlTextToPDF(string htmlText)
        {
            if (string.IsNullOrEmpty(htmlText))
            {
                return null;
            }
            //避免當htmlText無任何html tag標籤的純文字時,轉PDF時會掛掉,所以一律加上<p>標籤
            htmlText = "<p>" + htmlText + "</p>";

            MemoryStream outputStream = new MemoryStream();//要把PDF寫到哪個串流
            byte[] data = Encoding.UTF8.GetBytes(htmlText);//字串轉成byte[]
            MemoryStream msInput = new MemoryStream(data);
            Document doc = new Document();//要寫PDF的文件,建構子沒填的話預設直式A4
            PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);
            //指定文件預設開檔時的縮放為100%
            PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);
            //開啟Document文件 
            doc.Open();
            //使用XMLWorkerHelper把Html parse到PDF檔裡
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory());
            //將pdfDest設定的資料寫到PDF檔
            PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
            writer.SetOpenAction(action);
            doc.Close();
            msInput.Close();
            outputStream.Close();
            //回傳PDF檔案 
            return outputStream.ToArray();

        }

我有看一下路徑傳的值,我發現他回傳的不是我表單裡的內容
所以想請問這個路徑我該如何修改?

看更多先前的討論...收起先前的討論...
YoChen iT邦研究生 1 級 ‧ 2019-09-27 17:15:23 檢舉
不太明白您的問題~XDDD
如果URL沒錯的話,回傳的內容應該就不會錯才對~
可能要請您提供一下您接收到了什麼值~
tenno081 iT邦研究生 4 級 ‧ 2019-09-27 17:29:03 檢舉
就是下載下來是登入畫面的pdf
如果沒登出直接用這URL的話就是表單裡的畫面
登出後再使用這個URL就只有登入畫面這樣
YoChen iT邦研究生 1 級 ‧ 2019-09-27 18:04:56 檢舉
這樣的機制是正確的,
您必須處理authorize的問題才行~
小魚 iT邦大師 1 級 ‧ 2019-09-27 19:04:13 檢舉
這個機制聽起來很正常啊...
因為系統有做登入驗證.
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
小魚
iT邦大師 1 級 ‧ 2019-09-27 17:28:31

我沒用過 XMLWorkerHelper 跟 PdfAction,
無法回答你的問題.

tenno081 iT邦研究生 4 級 ‧ 2019-09-27 17:29:57 檢舉

那能否請問您適用哪個方式轉呢?
我是有試過Rotativa這個套件
不過結果一樣就是

小魚 iT邦大師 1 級 ‧ 2019-09-27 17:38:03 檢舉

我也是用iTextSharp,
不過我是用Table的方式輸出,
而且是單機的不是網頁的.

小魚 iT邦大師 1 級 ‧ 2019-09-27 17:39:30 檢舉

如果你是直接用網站請求的,
那就跟爬蟲有關係了,
看起來你要下載的可能是需要登入的頁面.
那是抓資料的問題,
不是iTextSharp的問題,
你先Check一下你抓到的資料是什麼.

0
YoChen
iT邦研究生 1 級 ‧ 2019-09-27 18:09:58

這樣看起來您需要處理的是Authorize的問題,
如果您想要讓您的ActionResult略過所有Authorize控制,
請在ActionResult上方標註AllowAnonymous

[HttpGet]
[AllowAnonymous]
public ActionResult ExportPDF(string strGuid)
{      
    // ...
}

我要發表回答

立即登入回答