Html Powershell,IText7 mangles表格td宽度(含百分比)
我有一个HTML页面,我想用iText7转换成PDF。这样,只有表格布局不等于HTML版本 原始HTML布局仅包含相关部分: 现在是创建的PDF文件。请检查“Oracle-JRE-8…”之前的部分: td宽度不同。表格宽度=100%,第一列为20%,第二列为80% 这是我用来创建PDF文件的PowerShell代码Html Powershell,IText7 mangles表格td宽度(含百分比),html,css,powershell,html-table,itext7,Html,Css,Powershell,Html Table,Itext7,我有一个HTML页面,我想用iText7转换成PDF。这样,只有表格布局不等于HTML版本 原始HTML布局仅包含相关部分: 现在是创建的PDF文件。请检查“Oracle-JRE-8…”之前的部分: td宽度不同。表格宽度=100%,第一列为20%,第二列为80% 这是我用来创建PDF文件的PowerShell代码 { <# .NOTES ===========================================================
{
<#
.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, Helvetica, 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, Helvetica, 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, Helvetica, 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
}
}
文件版本:
我的问题是如何确保所有表都具有相同的布局
和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, Helvetica, 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, Helvetica, sans-serif;
color: #e68a00;
font-size: 28px;
text-align: center;
}
h2 {
font-family: Arial, Helvetica, sans-serif;
color: #000099;
font-size: 16px;
text-align: center;
}
#Cred {
font-family: Arial, Helvetica, 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>
致以亲切的问候,
Willem Jan之所以会出现这种情况,是因为前三张表格中的一些单元格的最小宽度很大。表格不适合页面区域,因此其宽度超出预期。当我们减小显示宽度时,这种效果也存在于HTML中: 为避免出现这种情况,您可以选择以下选项之一: 更改输出文档页面大小或页面方向:
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);
用一张桌子代替几张桌子
使用字体大小减小内容的大小。例如,可以将表格元素的字体大小设置为14像素
这是因为前三个表中某些单元格的最小宽度很大。表格不适合页面区域,因此其宽度超出预期。当我们减小显示宽度时,这种效果也存在于HTML中: 为避免出现这种情况,您可以选择以下选项之一: 更改输出文档页面大小或页面方向:
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);
用一张桌子代替几张桌子
使用字体大小减小内容的大小。例如,可以将表格元素的字体大小设置为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);
}
}
前三个表中导致最小宽度问题的单元格包含单行文本,其文件路径包含许多字符。因此,另一个选项可以是按此字符拆分文本。您可以在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);
}
}
您可能希望从HTML中删除单个列宽,然后为表行的第一个子行添加一些CSS以使其不换行。类似这样的事情,我对HTML/CSS-tr td:first child{white space:nowrap;}不太了解。为了简化复制,您可以附加您试图转换的原始最小化HTML+CSS吗?@Ash:HTML似乎很好。我也尝试了css代码,但结果是一样的。完成后,请参阅修改后的帖子。您可能希望从HTML中删除单个列宽,然后为表行的第一个子行添加一些CSS以不换行。类似这样的事情,我对HTML/CSS-tr td:first child{white space:nowrap;}不太了解。为了简化复制,您可以附加您试图转换的原始最小化HTML+CSS吗?@Ash:HTML似乎很好。我也尝试了css代码,但结果是一样的。完成后,查看修改后的帖子。我使用了固定的像素宽度,而不是百分比。这是迄今为止最好的解决办法。正在创建不是解决方案的单个表:-我用固定的像素宽度代替了百分比。这是迄今为止最好的解决办法。正在创建不是解决方案的单个表:-虽然我不懂Java,但它可能对其他人有帮助。虽然我不懂Java,但它可能对其他人有帮助。