简体   繁体   English

PS - 如何将 output 保存到带有 colors 的文件中?

[英]PS - How to save output to file with colors?

How can I save the terminal output to a PDF file with colors?如何将终端 output 保存到 colors 的 PDF 文件中?

I use right now this command:我现在使用这个命令:

myCommand that output something with colors to terminal | Out-Printer -Name "Microsoft Print to PDF"

It does create the file, but colors are missing.它确实创建了文件,但缺少 colors。 Any ideas?有任何想法吗? Tips?尖端?

I'm using Windows 10, Powershell我正在使用 Windows 10、Powershell

I would heavily suggest using a module for doing that, gives you more control over the output most of the times.我强烈建议使用一个模块来做到这一点,在大多数情况下,您可以更好地控制 output。

http://www.powertheshell.com/modules/ http://www.powertheshell.com/modules/

Get the PDFTools Mod.获取 PDFTools Mod。

Out-PTSPdf -Name "YourFileName" -Path C:\Users\YourUser\Documents

Nevertheless, I doubt that PowerShell actually pipes "colored content" with colors, because it is the PowerShell Engine itself "coloring" according to the syntax.尽管如此,我怀疑 PowerShell 实际上用 colors 管道“着色内容”,因为它是 PowerShell 引擎本身根据语法“着色”。 Not sure about that tho.不确定。

You didn't say how your output commands look like, but...你没有说你的 output 命令是什么样子的,但是......
You could create an html file with colours and print this file as PDF, eg:您可以创建一个带有颜色的 html 文件并将此文件打印为 PDF,例如:

$HTMLFilePrefix = "<head></head><body>"
$HTMLFileSuffix = "<body>"
$HTMLStartRed = "<p><span style='color:red'>"
$HTMLEndRed = "</span></p>"
$HTMLFilePrefix + $HTMLStartRed + "This text is colored red" + $HTMLEndRed + $HTMLFileSuffix | Out-File -FilePath "C:\Temp\test.htm"

Maybe "ConvertTo-Html" can also help to create the html file...也许“ConvertTo-Html”也可以帮助创建 html 文件......

If you run your script in Windows Terminal Preview you can copy and paste the output (into Word for example) and it will preserve all the formatting.如果您在 Windows 终端预览中运行您的脚本,您可以复制并粘贴 output(例如粘贴到 Word 中),它将保留所有格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM