【发布时间】:2021-05-22 08:48:32
【问题描述】:
我有一个 HTML 页面,我想用 iText7 将其转换为 PDF。行得通,只是表格布局不等于HTML版本。
现在是创建的 PDF 文件。请检查 'Oracle-JRE-8 (...)' 之前的部分:
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, 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>
诚挚的问候, 威廉-扬
【问题讨论】:
-
您可能希望从 HTML 中删除各个列的宽度,然后为表格行的第一个子项添加一些 CSS 以使其不换行。像这样的东西,我对 HTML/CSS 不太了解 -
tr td:first-child { white-space: nowrap; } -
为方便复制,能否附上您要转换的原始最小化HTML+CSS?
-
@Ash:HTML 似乎没问题。我也尝试了
(使用 css 代码),但结果是一样的。完成,请参阅(修改后的)帖子。
标签: html css powershell html-table itext7