2016-01-22 69 views
2

有沒有一種方法可以在NReco中設置以毫米爲單位的打印頁面大小,就像您可以使用邊框一樣 - 在html中爲wkhtmltopdf的pdf包裝?找到一種方法來指定docs中的四種頁面大小之一,但這不夠精確。以NReco爲單位設置頁面大小(WkHtmlToPdf)

從閱讀wkhtmltopdf文檔本身來看,它似乎也限於預定義的頁面大小,而不是以長度爲單位手動設置它們。

提出這個問題,以防萬一我希望是錯的。例如,需要將頁面設置爲15x10cm。

編輯:我無法迫使圖書館使用html頁面設置(作爲在html中設置任何內容以及在NReco中設置高度/寬度/邊距的替代方法,如下面我的部分答案所示)。這:

@page { 
     size: 4in 3in; 
     margin: 0mm 0mm 0mm 0mm; 
    } 

實際工作中的打印,但是當我試圖迫使NReco與使用它:

pdfConverter.CustomWkHtmlArgs = "--print-media-type"; 

它什麼都不做。例子來自NReco網站的首頁,這使得它更有趣。

回答

1

望着從wkhtmltopdf文檔這個片段:

 --page-height <unitreal>  Page height 
    -s, --page-size <Size>    Set paper size to: A4, Letter, etc. 
             (default A4) 
     --page-width <unitreal>   Page width 

我要說的是,使用--page高度和--page寬度會做的伎倆。邏輯會說,這些將設置頁面的高度和寬度的點,所以你必須做數學轉換你的釐米或毫米爲PDF點(一英寸72點)。

+0

我正在使用NReco包裝。包裝器將頁面尺寸設置爲毫米或設置爲默認的「A4」尺寸,如上所示。我把你的答案提高了,因爲我確信它可以在wkhtml本身中工作,但是我仍然無法正確地渲染東西(請參閱我的答案),所以我現在就把它打開。 – VSO

0

注意:如果我到某個地方,我會編輯這個答案,但是現在,我猜想任何人都可能讀這個東西比什麼都好。

發佈一個答案,其他人可能會發現有用的,但不是完整的解決我的問題:

HtmlToPdfConverter pdfConverter = new HtmlToPdfConverter(); 

//The page width and page height values are in mm 
pdfConverter.PageWidth = 102; 
pdfConverter.PageHeight = 77; 

這不應該被接受的答案 - 由於某種原因,它不完全匹配簡單的HTML上漿。例如,如果我創建一個尺寸比率爲4x3的html文檔,然後適當地設置這些道具,頁面上的結果圖像仍然不會佔用整個頁面(最終變小)。

如果我運行下面的HTML和設置頁面大小到102毫米×72毫米我得到下面的截圖,這是路要走,儘管具有比右:

<html> 
<head> 
    <style> 
     .reportBody { 
      padding: 0px; 
      border: 0px; 
      margin: 0px; 
     } 

     .reportTable{ 
      padding: 0px; 
      border: 0px; 
      margin: 0px; 
      width: 102mm; 
      height: 77mm; 
     } 
    </style> 
</head> 

<body class = "reportBody"> 
    <table class = "reportTable"> 
     <tr> 
      <td style = "background-color:red"> 
       Row 1 Column 1 
      </td> 
      <td style = "background-color:blue"> 
       Row 1 Column 2 
      </td> 
     </tr> 
     <tr> 
      <td style = "background-color:green"> 
       Row 2 Column 1 
      </td> 
      <td style = "background-color:yellow"> 
       Row 2 Column 2 
      </td> 
     </tr> 
    </table> 
</body> 

</html> 

enter image description here

1

快速回答您的問題是肯定的,當您使用heightwidth屬性時,設置在mm中,所以您在做什麼對於C#代碼是正確的,您的尺寸問題是轉換器採用了「智能」大小調整技術默認。這個比例是正確的,但減少的尺寸與我的問題完全相同,這是通過包含--disable-smart-shrinking選項解決的。

如需更全面的照片: 我剛剛完成了一項直接從新Razor應用程序獲得處方打印的步驟,這些PDF是通過NReco包裝生成的。我必須打印一張固定的處方單(215毫米x 176毫米),並在其周圍留有一點空白。這是我在控制器中獲得的代碼,它返回pdf作爲fileResult變量。

public async Task<ActionResult> OutputScript(int? id) 
    { 
     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 

     var model = await GetViewModelForScript(id.Value); 
     if (model == null) 
     { 
      return HttpNotFound(); 
     } 

     // create a string writer to receive the HTML code 
     StringWriter stringWriter = new StringWriter(); 

     // get the view to render 
     ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, "Script", null); 
     // create a context to render a view based on a model 
     ViewContext viewContext = new ViewContext(
       ControllerContext, 
       viewResult.View, 
       new ViewDataDictionary(model), 
       new TempDataDictionary(), 
       stringWriter 
       ); 

     // render the view to a HTML code 
     viewResult.View.Render(viewContext, stringWriter); 

     // return the HTML code 
     string htmlToConvert = stringWriter.ToString(); 

     // instantiate the HTML to PDF converter 
     HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); 
     htmlToPdfConverter.CustomWkHtmlArgs = " --print-media-type --title \"SMS Script " + model.ScriptID + "\" --dpi 300 --disable-smart-shrinking"; 
     htmlToPdfConverter.PageHeight = 215; 
     htmlToPdfConverter.PageWidth = 176; 
     var margins = new PageMargins(); 
     margins.Bottom = 4; 
     margins.Top = 4; 
     margins.Left = 5; 
     margins.Right = 5; 
     htmlToPdfConverter.Margins = margins; 
     htmlToPdfConverter.Orientation = PageOrientation.Landscape; 
     // render the HTML code as PDF in memory 
     byte[] pdfBuffer = htmlToPdfConverter.GeneratePdf(htmlToConvert); 

     // send the PDF file to browser 
     FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); 
     fileResult.FileDownloadName = "Script.pdf"; 

     return fileResult; 

    } 

現在所有@media print指令得到遵守,隱藏,顯示當上打印生成PDF屏幕「預覽」標籤的方式。

+0

我通過設置縮放設置而不是禁用智能收縮來「解決」了問題:converter.Zoom = 2;兩個只是在這裏2x。我必須分別爲amazon服務器,azure服務器和本地機器配置縮放設置,因爲顯然它基於內置在圖形系統中的機器(不問)。我也寫了自己的幫手,將註釋放在PDF轉換器可以看到的html中。如果這是你會發現有用的東西,讓我知道。隨着您和我嘗試的一切,我仍然無法獲得非常小的利潤率,無需黑客即可進行正確打印。 – VSO