Powershell,IText7 用百分比处理表格 td 宽度

问题描述

我有一个 HTML 页面,我想用 iText7 将其转换为 PDF。那行得通,只有表格布局不等于 HTML 版本。

原始HTML布局(仅相关部分):

HTML layout

现在是创建的 PDF 文件。请检查'Oracle-JRE-8 (...)'之前的部分:

PDF file

td 宽度不同。表格宽度=100%,第一列是20%,第二列是80%。

这是我用来创建 PDF 文件的 PowerShell 代码

   {

    <#
    .NOTES
    =============================================================================================================================================
    Created with:     Windows PowerShell ISE
    Created on:       02-February-2021
    Created by:       Willem-Jan
    Organization:     
    Functionname:     Write-HTMLFile
    =============================================================================================================================================
    .SYnopSIS

    This function creates an HTML file. 

    #>

    param
     (
      [String] $HTMLFiletoWrite,[Switch] $SaveAsPDFFile
     )

  # =============================================================================================================================================
  # Define the CSS file.
  # =============================================================================================================================================
    
    $Header      = ""
    $CSS         = "<style type=$([char]34)text/css$([char]34)>`n"
    $CSS        += "table              {`n"
    $CSS        += "                    font-size:      16px;`n"
    $CSS        += "                    border:         1px solid #395870;`n"
    $CSS        += "                    font-family:    Arial,Helvetica,sans-serif;`n"
    $CSS        += "                   }`n`n"
    $CSS        += "td                 {`n"
    $CSS        += "                    padding:        5px;`n"
    $CSS        += "                    margin:         0px;`n"
    $CSS        += "                    border:         1px solid #395870;`n"
    $CSS        += "                    max-width:      700px;`n"
    $CSS        += "                    vertical-align: top;`n"
    $CSS        += "                   }`n`n"
    $CSS        += "th                 {`n"
    $CSS        += "                    background:     #395870;`n"
    $CSS        += "                    background:     linear-gradient(#49708f,#293f50);`n"
    $CSS        += "                    color:          #fff;`n"
    $CSS        += "                    font-size:      14px;`n"
    $CSS        += "                    padding:        10px 15px;`n"
    $CSS        += "                    vertical-align: top;`n"
    $CSS        += "                    border:         1px solid #395870;`n"
    $CSS        += "                   }`n`n"
    $CSS        += "tr                 {`n"
    $CSS        += "                    width:          1000px;`n"
    $CSS        += "                   }`n`n"
    $CSS        += "h1                 {`n"
    $CSS        += "                    font-family:    Arial,sans-serif;`n"
    $CSS        += "                    color:          #e68a00;`n"
    $CSS        += "                    font-size:      28px;`n"
    $CSS        += "                    text-align:     center;`n"
    $CSS        += "                   }`n`n"
    $CSS        += "h2                 {`n"
    $CSS        += "                    font-family:    Arial,sans-serif;`n"
    $CSS        += "                    color:          #000099;`n"
    $CSS        += "                    font-size:      16px;`n"
    $CSS        += "                    text-align:     center;`n"
    $CSS        += "                   }`n`n"
    $CSS        += "#Cred              {`n"
    $CSS        += "                    font-family:    Arial,sans-serif;`n"
    $CSS        += "                    color:          #0000ff;`n"
    $CSS        += "                    font-size:      12px;`n"
    $CSS        += "                    text-align:     left;`n"
    $CSS        += "                   }`n`n"
    $CSS        += "tr:nth-child(even) {`n"
    $CSS        += "                    background:     #CCC;`n"
    $CSS        += "                   }`n`n"
    $CSS        += "tr:nth-child(odd)  {`n"
    $CSS        += "                    background:     #FFF;`n"
    $CSS        += "                   }`n`n"
    $CSS        += "</style>"
    $CSS      += ""

  # =============================================================================================================================================
  # Replace the `n with <br> in varIoUs columns. So there is a proper table layout.
  # Do some cleanup afterwards.
  # =============================================================================================================================================

    ForEach ($Record in $Global:gblarrTable)
     {
      If($Record."Shortcuts")
       {
        $Record."Shortcuts" = $Record."Shortcuts" -replace "`n","<br><br>"
       }
      If($Record."Connection group")
       {
        $Record."Connection group" = $Record."Connection group" -replace "`n","<br><br>"
       }
      If($Record."Deployment config files")
       {
        $Record."Deployment config files" = $Record."Deployment config files" -replace "`n","<br><br>"
       }
      If($Record."Package and version ID")
       {
        $Record."Package and version ID" = $Record."Package and version ID" -replace "`n","<br><br>"
       }
     }

    ForEach ($Record in $Global:gblarrConnectionGroups)
     {
      If($Record."Packages as a part of the connection group")
       {
        $Record. "Packages as a part of the connection group" = $Record. "Packages as a part of the connection group" -replace "`n","<br><br>"
       }
     }
 
    $HTMLTable = ""
    if($SaveAsPDFFile)
     {
      $Global:gblarrTable = $Global:gblarrTable |sort-object -Property "display Name"
      ForEach ($Record in $Global:gblarrTable)
       {
        $HTMLTable   += $Record | ConvertTo-Html -Fragment -As List
        $HTMLTable   += "<br>"
       }
       $HTMLTable = $HTMLTable -replace ("<table>","<table width=100%>")
       $HTMLTable = $HTMLTable -replace ("<tr><td>","<tr><td width=20%>")
       $HTMLTable = $HTMLTable -replace ("</td><td>","</td><td width=80%>")
       $HTMLTable = $HTMLTable -replace ("</table>","</table>`n")
     }
      else
     {
      $HTMLTable   = $Global:gblarrTable |sort-object -Property "display Name" | ConvertTo-Html -Fragment
     }
    $HTMLTable   = [System.Web.HttpUtility]::HtmlDecode($HTMLTable)
   
    $Title       = "<h1>Overview of all the AppV Packages published on the computer $($Global:gblComputerName)</h1>"
    $Body        = "$Title<h2>Table</h2>$HTMLTable"

  # =============================================================================================================================================
  # If applicable: add information about the connection groups to the HTML page.
  # =============================================================================================================================================

    if($Global:gblarrConnectionGroups.Count -gt 0)
     {
      if($Global:gblarrConnectionGroups.Count -gt 1)
       {
        $Global:gblarrConnectionGroups = $Global:gblarrConnectionGroups | sort-object -Property Priority
       }

      if($SaveAsPDFFile)
       {
        $HTMLConnectionGroupOverview = ""
        ForEach ($Record in $Global:gblarrConnectionGroups)
         {
          $HTMLConnectionGroupOverview += $Record | ConvertTo-Html -Fragment -As List
          $HTMLConnectionGroupOverview += "<br>"
         }
       }
        else
       {
        $HTMLConnectionGroupOverview = $Global:gblarrConnectionGroups | ConvertTo-Html -Fragment
       }
      $HTMLConnectionGroupOverview = [System.Web.HttpUtility]::HtmlDecode($HTMLConnectionGroupOverview)
      $Body +="<br><h2>Overview of the connection groups.</h2><br>$HTMLConnectionGroupOverview"
     }

  # =============================================================================================================================================
  # If applicable: add information about globally enabled or disabled subsystems to the HTML page.
  # =============================================================================================================================================

    if($Global:gblarrEnableddisabled.Count -gt 0)
     {
      if($Global:gblarrEnableddisabled.Count -gt 1)
       {
        $Global:gblarrEnableddisabled = $Global:gblarrEnableddisabled | sort-object -Property "Subsystem Name"
       }
      if($SaveAsPDFFile)
       {
        $HTMLEnabledOrdisabledSubsystems = ""
        ForEach ($Record in $Global:gblarrEnableddisabled)
         {
          $HTMLEnabledOrdisabledSubsystems += $Record | ConvertTo-Html -As List -Fragment
          $HTMLEnabledOrdisabledSubsystems += "<br>"
         }
       }
        else
       {
        $HTMLEnabledOrdisabledSubsystems = $Global:gblarrEnableddisabled | ConvertTo-Html -Fragment
       }
       
      $HTMLEnabledOrdisabledSubsystems = [System.Web.HttpUtility]::HtmlDecode($HTMLEnabledOrdisabledSubsystems)
      $Body +="<br><h2>Overview of the disabled and enabled subsystems.</h2><br>$HTMLEnabledOrdisabledSubsystems"
     }

  # =============================================================================================================================================
  # If applicable: add information about the used parameters to the HTML page.
  # =============================================================================================================================================


    if($TableWithParameters.Count -gt 0)
     {
      if($SaveAsPDFFile)
       {
        $HTMLParameterstable = ""
        ForEach ($Record in $TableWithParameters)
         {
          $HTMLParameterstable += $Record | ConvertTo-Html -As List -Fragment
          $HTMLParameterstable += "<br>"
         }
       }
        else
       {
        $HTMLParameterstable = $TableWithParameters | ConvertTo-Html -Fragment
       }

      $Body +="<br><h2>Used parameters for the script.</h2><br>$HTMLParameterstable"
     }

  # =============================================================================================================================================
  # Load all the DLL files that are needed for IText7 HTML To PDF
  # See https://kb.itextsupport.com/home/it7kb/ebooks/itext-7-converting-html-to-pdf-with-pdfhtml for more information.
  # =============================================================================================================================================
  
    if($SaveAsPDFFile)
     {
      $DLLPath  = $(Split-Path $script:MyInvocation.MyCommand.Path) + "\DLL"
      $DLLFiles = Get-ChildItem -Path $DLLPath -Filter *.dll -File
      ForEach ($DLLFile in $DLLFiles)
       {
        Add-Type -Path $($DLLFile.FullName)
        Add-EntryToLogFile "The DLL file $($DLLFile.FullName) has been loaded successfully." 
       }
     }

  # =============================================================================================================================================
  # The footer and create the HTML page. 
  # =============================================================================================================================================

    $Timestamp   = (Get-Date -UFormat "%a %e %b %Y %X").ToString()
    $PostContent = "<p id=$([char]34)Cred$([char]34)>Creation Date: $Timestamp</p>"
    $Report      = ConvertTo-Html -Head $CSS -Body $Body -PostContent $PostContent

    if($SaveAsPDFFile)
     {
      $PDFFiletoWrite = [System.IO.FileInfo]::new($HTMLFiletoWrite -replace("html","pdf"))
      $Report | Out-File $HTMLFiletoWrite
      $HTMLInputFile  = [System.IO.FileInfo]::new($HTMLFiletoWrite)
      [iText.Html2Pdf.HtmlConverter]::ConvertToPdf($HTMLInputFile,$PDFFiletoWrite)
      $tmpLine = "The PDF file '$PDFFiletoWrite' has been created."
      Add-EntryToLogFile -Entry $tmpLine
      Write-Host $tmpLine
      #Remove-Item -Path $HTMLFiletoWrite
     }
      else
     {
      $Report | Out-File $HTMLFiletoWrite
      $tmpLine = "The webpage '$HTMLFiletoWrite' has been created."
      Add-EntryToLogFile -Entry $tmpLine
      Write-Host $tmpLine
     }
   }

文件版本:

DLL files and its versions.

我的问题是我可以做些什么来确保所有表格都具有相同的布局。

和 html 文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
table              {
                    font-size:      16px;
                    border:         1px solid #395870;
                    font-family:    Arial,sans-serif;
                   }

td                 {
                    padding:        5px;
                    margin:         0px;
                    border:         1px solid #395870;
                    max-width:      700px;
                    vertical-align: top;
                   }

th                 {
                    background:     #395870;
                    background:     linear-gradient(#49708f,#293f50);
                    color:          #fff;
                    font-size:      14px;
                    padding:        10px 15px;
                    vertical-align: top;
                    border:         1px solid #395870;
                   }

tr                 {
                    width:          1000px;
                   }

h1                 {
                    font-family:    Arial,sans-serif;
                    color:          #e68a00;
                    font-size:      28px;
                    text-align:     center;
                   }

h2                 {
                    font-family:    Arial,sans-serif;
                    color:          #000099;
                    font-size:      16px;
                    text-align:     center;
                   }

#Cred              {
                    font-family:    Arial,sans-serif;
                    color:          #0000ff;
                    font-size:      12px;
                    text-align:     left;
                   }

tr:nth-child(even) {
                    background:     #CCC;
                   }

tr:nth-child(odd)  {
                    background:     #FFF;
                   }

</style>
</head><body>
<h1>Overview of all the AppV Packages published on the computer LAPTOPWILLEMJAN</h1><h2>Table</h2><table width=100%> <tr><td width=20%>display Name:</td><td width=80%>Irfanskiljan-irfanview-4.50-NL-R001</td></tr> <tr><td width=20%>Package and version ID:</td><td width=80%>ff9d05b6-1290-4b07-83e7-755756ae619b<br><br>570ac062-b3f7-4a79-b4f7-0c682f49fbf0</td></tr> <tr><td width=20%>Package Version:</td><td width=80%>0.0.0.10</td></tr> <tr><td width=20%>Package Description:</td><td width=80%>No description entered</td></tr> <tr><td width=20%>Published globally:</td><td width=80%>False</td></tr> <tr><td width=20%>Published to user:</td><td width=80%>True</td></tr> <tr><td width=20%>Shortcuts:</td><td width=80%></td></tr> <tr><td width=20%>Full VFS Write Mode:</td><td width=80%>true</td></tr> <tr><td width=20%>Deployment config files:</td><td width=80%>C:\ProgramData\App-V\ff9d05b6-1290-4b07-83e7-755756ae619b\570ac062-b3f7-4a79-b4f7-0c682f49fbf0\AppxManifest.xml<br><br>C:\Users\Willem-JanVroom\AppData\Roaming\Microsoft\AppV\Client\Catalog\Packages\{FF9D05B6-1290-4B07-83E7-755756AE619B}\{570AC062-B3F7-4A79-B4F7-0C682F49FBF0}\UserManifest.xml</td></tr> <tr><td width=20%>Shortcuts Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Filetype associatons Enabled:</td><td width=80%></td></tr> <tr><td width=20%>URLProtocols Enabled:</td><td width=80%></td></tr> <tr><td width=20%>COM Mode:</td><td width=80%></td></tr> <tr><td width=20%>InProcess Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Out of Process Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Objects Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Registry Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Filesystem Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Fonts Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Environment variables Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Services Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Connection group:</td><td width=80%></td></tr> </table>
<br><table width=100%> <tr><td width=20%>display Name:</td><td width=80%>MProof-Clientele-2020R2-NL-R001</td></tr> <tr><td width=20%>Package and version ID:</td><td width=80%>e46b2a8f-31b9-498a-95fd-321455a50ed6<br><br>c4f37c12-6063-4106-9ab8-22d7bb87a35a</td></tr> <tr><td width=20%>Package Version:</td><td width=80%>0.0.0.1</td></tr> <tr><td width=20%>Package Description:</td><td width=80%>No description entered</td></tr> <tr><td width=20%>Published globally:</td><td width=80%>False</td></tr> <tr><td width=20%>Published to user:</td><td width=80%>True</td></tr> <tr><td width=20%>Shortcuts:</td><td width=80%>File:        [{AppVPackageRoot}]\Clientele ITSM 2020.2\Client\Shortcut\Clientele 2020.2 Admin.lnk<br><br>Target:      [{AppVPackageRoot}]\Clientele ITSM 2020.2\Client\Clientele.Loader.exe -language nl-NL -nodownload<br><br>Working Dir: [{AppVPackageRoot}]\Clientele ITSM 2020.2\Client<br><br><br><br>File:        [{AppVPackageRoot}]\Clientele ITSM 2020.2\Client\Shortcut\Clientele 2020.2.lnk<br><br>Target:      [{AppVPackageRoot}]\Clientele ITSM 2020.2\Client\Clientele.Loader.exe -language nl-NL -nodownload -UseWindowsCredentials<br><br>Working Dir: [{AppVPackageRoot}]\Clientele ITSM 2020.2\Client</td></tr> <tr><td width=20%>Full VFS Write Mode:</td><td width=80%>true</td></tr> <tr><td width=20%>Deployment config files:</td><td width=80%>C:\ProgramData\App-V\e46b2a8f-31b9-498a-95fd-321455a50ed6\c4f37c12-6063-4106-9ab8-22d7bb87a35a\AppxManifest.xml<br><br>C:\Users\Willem-JanVroom\AppData\Roaming\Microsoft\AppV\Client\Catalog\Packages\{E46B2A8F-31B9-498A-95FD-321455A50ED6}\{C4F37C12-6063-4106-9AB8-22D7BB87A35A}\UserManifest.xml<br><br>C:\Users\Willem-JanVroom\AppData\Roaming\Microsoft\AppV\Client\Catalog\Packages\{E46B2A8F-31B9-498A-95FD-321455A50ED6}\{C4F37C12-6063-4106-9AB8-22D7BB87A35A}\DynamicConfiguration.xml</td></tr> <tr><td width=20%>Shortcuts Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Filetype associatons Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>URLProtocols Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>COM Mode:</td><td width=80%>Integrated</td></tr> <tr><td width=20%>InProcess Enabled:</td><td width=80%>false</td></tr> <tr><td width=20%>Out of Process Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Objects Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Registry Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Filesystem Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Fonts Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Environment variables Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Services Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Connection group:</td><td width=80%></td></tr> </table>
<br><table width=100%> <tr><td width=20%>display Name:</td><td width=80%>Oracle-JRE-8.0.2610.12-EN-1.0.0</td></tr> <tr><td width=20%>Package and version ID:</td><td width=80%>77c22880-619b-4b2d-94bb-4cefa3bdf7ff<br><br>1e6ef32e-ebab-43fc-913b-c98e9f1b9e5b</td></tr> <tr><td width=20%>Package Version:</td><td width=80%>0.0.0.4</td></tr> <tr><td width=20%>Package Description:</td><td width=80%>Willem-Jan Vroom</td></tr> <tr><td width=20%>Published globally:</td><td width=80%>True</td></tr> <tr><td width=20%>Published to user:</td><td width=80%>False</td></tr> <tr><td width=20%>Shortcuts:</td><td width=80%></td></tr> <tr><td width=20%>Full VFS Write Mode:</td><td width=80%>true</td></tr> <tr><td width=20%>Deployment config files:</td><td width=80%>C:\ProgramData\App-V\77c22880-619b-4b2d-94bb-4cefa3bdf7ff\1e6ef32e-ebab-43fc-913b-c98e9f1b9e5b\AppxManifest.xml<br><br>C:\ProgramData\Microsoft\AppV\Client\Catalog\Packages\{77C22880-619B-4B2D-94BB-4CEFA3BDF7FF}\{1E6EF32E-EBAB-43FC-913B-C98E9F1B9E5B}\Manifest.xml</td></tr> <tr><td width=20%>Shortcuts Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Filetype associatons Enabled:</td><td width=80%></td></tr> <tr><td width=20%>URLProtocols Enabled:</td><td width=80%></td></tr> <tr><td width=20%>COM Mode:</td><td width=80%>Integrated</td></tr> <tr><td width=20%>InProcess Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Out of Process Enabled:</td><td width=80%>true</td></tr> <tr><td width=20%>Objects Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Registry Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Filesystem Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Fonts Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Environment variables Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Services Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Connection group:</td><td width=80%></td></tr> </table>
<br><table width=100%> <tr><td width=20%>display Name:</td><td width=80%>SumatraSoftware-Sumatra2020-5.1.8590-EN-VWS-CIBG-R001</td></tr> <tr><td width=20%>Package and version ID:</td><td width=80%>7c5baaed-c594-4bbf-8ec1-3951f20b66ed<br><br>18525867-b490-4d14-b98a-30f8f4634a33</td></tr> <tr><td width=20%>Package Version:</td><td width=80%>0.0.0.6</td></tr> <tr><td width=20%>Package Description:</td><td width=80%>Gemaakt door Willem-Jan Vroom</td></tr> <tr><td width=20%>Published globally:</td><td width=80%>True</td></tr> <tr><td width=20%>Published to user:</td><td width=80%>False</td></tr> <tr><td width=20%>Shortcuts:</td><td width=80%></td></tr> <tr><td width=20%>Full VFS Write Mode:</td><td width=80%>true</td></tr> <tr><td width=20%>Deployment config files:</td><td width=80%>C:\ProgramData\App-V\7c5baaed-c594-4bbf-8ec1-3951f20b66ed\18525867-b490-4d14-b98a-30f8f4634a33\AppxManifest.xml<br><br>C:\ProgramData\Microsoft\AppV\Client\Catalog\Packages\{7C5BAAED-C594-4BBF-8EC1-3951F20B66ED}\{18525867-B490-4D14-B98A-30F8F4634A33}\Manifest.xml</td></tr> <tr><td width=20%>Shortcuts Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Filetype associatons Enabled:</td><td width=80%></td></tr> <tr><td width=20%>URLProtocols Enabled:</td><td width=80%></td></tr> <tr><td width=20%>COM Mode:</td><td width=80%></td></tr> <tr><td width=20%>InProcess Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Out of Process Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Objects Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Registry Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Filesystem Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Fonts Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Environment variables Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Services Enabled:</td><td width=80%></td></tr> <tr><td width=20%>Connection group:</td><td width=80%></td></tr> </table>
<br><br><h2>Overview of the disabled and enabled subsystems.</h2><br><table> <tr><td>Subsystem name:</td><td>Virtual COM</td></tr> <tr><td>Enabled:</td><td>True</td></tr> <tr><td>disabled:</td><td></td></tr> </table><br><table> <tr><td>Subsystem name:</td><td>Virtual Objects</td></tr> <tr><td>Enabled:</td><td></td></tr> <tr><td>disabled:</td><td>True</td></tr> </table><br>
<table>
</table>
<p id="Cred">Creation Date: Sat 22 May 2021 00:43:01</p>
</body></html>

亲切的问候, 威廉-简

解决方法

发生这种情况是因为前三个表格中某些单元格的最小宽度很大。表格不适合页面区域,因此宽度超出预期。当我们减小显示宽度时,这种效果也存在于 HTML 中:https://i.stack.imgur.com/nFmiC.png

为避免这种情况,您可以选择以下选项之一:

  1. 更改输出文档页面大小或页面方向:

    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileInfo(outFilename)));
    pdfDocument.SetDefaultPageSize(PageSize.A3);
    // pdfDocument.SetDefaultPageSize(PageSize.A4.Rotate());
    HtmlConverter.ConvertToPdf(new FileStream(srcFilename,FileMode.Open),pdfDocument);
    
  2. 使用一张桌子而不是几张桌子。

  3. 使用 font-size 减小内容的大小。例如,您可以将表格元素的字体大小设置为 14 像素。

,

前三个表中导致最小宽度问题的单元格包含单行文本,文件路径包含许多 '' 字符。因此,另一个选项可以是按此字符拆分文本。您可以为此使用此代码示例(在 Java 上):

public static void main(String[] args) {
    ConverterProperties cp = new ConverterProperties();
    cp.setCssApplierFactory(new CustomCssApplierFactory());
    HtmlConverter.convertToPdf(new File(srcFilename),new File(outFilename),cp);
}

private static class CustomCssApplierFactory implements ICssApplierFactory {
    private ICssApplierFactory defaultCssApplierFactory = new DefaultCssApplierFactory();

    @Override
    public ICssApplier getCssApplier(IElementNode tag) {
        ICssApplier defaultCssApplier = defaultCssApplierFactory.getCssApplier(tag);
        return defaultCssApplier != null ? new CustomCssApplier(defaultCssApplier) : null;
    }
}

private static class CustomCssApplier implements ICssApplier {
    private final ICssApplier defaultCssApplier;

    public CustomCssApplier(ICssApplier defaultCssApplier) {
        this.defaultCssApplier = defaultCssApplier;
    }

    @Override
    public void apply(ProcessorContext context,IStylesContainer stylesContainer,ITagWorker tagWorker) {
        defaultCssApplier.apply(context,stylesContainer,tagWorker);
        IPropertyContainer elementResult = tagWorker.getElementResult();
        if (elementResult != null) {
            setCustomSplitCharacter(elementResult);
        } else if (tagWorker instanceof SpanTagWorker) {
            // If current element is span,then set the custom split character to nested elements
            for (IPropertyContainer ownLeafElement : ((SpanTagWorker) tagWorker).getOwnLeafElements()) {
                setCustomSplitCharacter(ownLeafElement);
            }
        }
    }

    private void setCustomSplitCharacter(IPropertyContainer elementResult) {
        // If Property.SPLIT_CHARACTERS was null then DefaultSplitCharacters class would be used during layout.
        Object property = elementResult.getProperty(Property.SPLIT_CHARACTERS);

        //  BreakAllSplitCharacters and KeepAllSplitCharacters instances can be set
        //  if CSS word-break property was applied.
        if (!(property instanceof BreakAllSplitCharacters)) {
            elementResult.setProperty(Property.SPLIT_CHARACTERS,new CustomSlashSplitCharacters());
        }
    }
}

private static class CustomSlashSplitCharacters extends DefaultSplitCharacters {
    private static final String SPLIT_CHARACTER = "\\";

    @Override
    public boolean isSplitCharacter(GlyphLine text,int glyphPos) {
        Glyph glyph = text.get(glyphPos);
        return glyph.hasValidUnicode() && SPLIT_CHARACTER.equals(glyph.getUnicodeString())
                || super.isSplitCharacter(text,glyphPos);
    }
}