使用PowerShell读取来自TXT文件的URL列表,并将每个URL保存到PDF,并用URL的最后一部分名称保存到PDF
以下是powershell代码,用于读取TXT文件的URL并将每个URL保存到PDF。这里每个URL都保存为number.pdf。我希望每个PDF都以URL的最后一部分命名。
对于ex:如果URL为' https://wwww.prodevelodertoriorarorial.com /lte-chapter-1-lte-Instruction/',我希望保存的pdf文件是'lte-chapter-chapter-1-lte-indroduction.pdf'
我从网站上获得了代码。任何人都可以根据我的要求对其进行修改。
$sourceFile = "D:\BATCH-PRINT-WEBPAGES-PDF\D\1\links2.txt" # the source file containing the URLs you want to convert
$destFolder = "D:\BATCH-PRINT-WEBPAGES-PDF\sharednotes\" # converted PDFs will be saved here. Folder has to exist.
$num = 0
foreach($link in [System.IO.File]::ReadLines($sourceFile))
{
$num++
$outfile = $num.ToString() + '.pdf'
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destFolder $outfile" "$link"
Start-Sleep -s 3
}
从我能够在互联网上聚集的东西,我做了以下操作:
$sourceFile = "D:\BATCH-PRINT-WEBPAGES-PDF\Version 1\linktst.txt" # the source file containing the URLs you want to convert
$destFolder = "D:\BATCH-PRINT-WEBPAGES-PDF\Version 1\OP\" # converted PDFs will be saved here. Folder has to exist.
$links= Get-Content -Path D:\BATCH-PRINT-WEBPAGES-PDF\Version1\linktst.txt
$num = 0
foreach($l in $links)
{
z=[uri]'l'
$nam = z.segment[-2]
$num++
$outfile = $nam.ToString() + '.pdf'
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destFolder $outfile" "$link"
Start-Sleep -s 3
}
它不起作用。
文本文件中的每个条目都是行。
https://www.prodeerveriatorial.com/lte-chapter-chapter-chapter-chapter-chapter-1-lte - 介绍/ https://wwwww.prodevelodertorial.com/lte-network-work-network-work-work-network-architection https:// www .prodevelopertutorial.com/4g-lte-tutorial-brief-working-of-network-elements-in-lte-architecture/ https://wwww.prodevervenitior.com/introdorial.com/introdaction-to-to- -e-utran-network-Architecture-elements/ https://www.prodeplacyertertion.com/introdeveration.com/introdeverion.com/introduction to-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-epc-eppc -network-architecture-elements/
每个URL都在文本中的新行中。
The following is a powershell code to read urls from txt file and save each url to a pdf. Here each url is saved as number.pdf. I want each pdf to be named with the last part of the url.
for ex: if a url is ' https://www.prodevelopertutorial.com/lte-chapter-1-lte-introduction/ ', I want the saved pdf file to be ' lte-chapter-1-lte-introduction.pdf '
I have obtained the code from a website. Can anybody please modify it as per my requirements.
$sourceFile = "D:\BATCH-PRINT-WEBPAGES-PDF\D\1\links2.txt" # the source file containing the URLs you want to convert
$destFolder = "D:\BATCH-PRINT-WEBPAGES-PDF\sharednotes\" # converted PDFs will be saved here. Folder has to exist.
$num = 0
foreach($link in [System.IO.File]::ReadLines($sourceFile))
{
$num++
$outfile = $num.ToString() + '.pdf'
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destFolder $outfile" "$link"
Start-Sleep -s 3
}
From what i was able to gather around the internet, i did the following:
$sourceFile = "D:\BATCH-PRINT-WEBPAGES-PDF\Version 1\linktst.txt" # the source file containing the URLs you want to convert
$destFolder = "D:\BATCH-PRINT-WEBPAGES-PDF\Version 1\OP\" # converted PDFs will be saved here. Folder has to exist.
$links= Get-Content -Path D:\BATCH-PRINT-WEBPAGES-PDF\Version1\linktst.txt
$num = 0
foreach($l in $links)
{
z=[uri]'l'
$nam = z.segment[-2]
$num++
$outfile = $nam.ToString() + '.pdf'
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destFolder $outfile" "$link"
Start-Sleep -s 3
}
Its not working.
each entry in the text file is a line.
https://www.prodevelopertutorial.com/lte-chapter-1-lte-introduction/
https://www.prodevelopertutorial.com/lte-network-architecture/
https://www.prodevelopertutorial.com/4g-lte-tutorial-brief-working-of-network-elements-in-lte-architecture/
https://www.prodevelopertutorial.com/introduction-to-e-utran-network-architecture-elements/
https://www.prodevelopertutorial.com/introduction-to-epc-network-architecture-elements/
each url is in a new line in the text.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的代码中,您使用
$ outfile = $ nam.tostring() +'.pdf'
您将$ NAM值声明为0,并增加每个循环的数字。当文件与数字创建时。
您可以在下面尝试。我的机器中没有Chrome.xe,因此没有测试过外档的创建。
In your Code you are using
$outfile = $nam.ToString() + '.pdf'
You declared $nam value as 0 and increasing the number for each loop. where as files are creating with the number.
You can try below. I don't have the chrome.xe in my machine so didn't tested the outfile creation.