使用PowerShell读取来自TXT文件的URL列表,并将每个URL保存到PDF,并用URL的最后一部分名称保存到PDF

发布于 2025-01-22 11:01:20 字数 3027 浏览 0 评论 0原文

以下是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2025-01-29 11:01:20

在您的代码中,您使用$ outfile = $ nam.tostring() +'.pdf'

您将$ NAM值声明为0,并增加每个循环的数字。当文件与数字创建时。

您可以在下面尝试。我的机器中没有Chrome.xe,因此没有测试过外档的创建。

$srcfile = "E:\Workspace\Test\Test.txt"
$destloc = "E:\Workspace\Test\Dest\"

$data = Get-Content $srcfile
foreach($url in $data){
    #Write-Output $url

    $url_trim = $url.Trim()
    if($url_trim.EndsWith("/"))
    {
        $url_trim = $url_trim.Substring(0,$url_trim.Length -1 )
    }
    #Write-Host $url_trim -ForegroundColor Cyan
    $filename = $url_trim.Substring($url_trim.LastIndexOf("/")+1)
    #Write-Output $filename

    $outfile = "$filename.pdf" 
    & 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destloc $outfile" "$url"
    Start-Sleep -s 3
    #Write-Output $outfile

}

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.

$srcfile = "E:\Workspace\Test\Test.txt"
$destloc = "E:\Workspace\Test\Dest\"

$data = Get-Content $srcfile
foreach($url in $data){
    #Write-Output $url

    $url_trim = $url.Trim()
    if($url_trim.EndsWith("/"))
    {
        $url_trim = $url_trim.Substring(0,$url_trim.Length -1 )
    }
    #Write-Host $url_trim -ForegroundColor Cyan
    $filename = $url_trim.Substring($url_trim.LastIndexOf("/")+1)
    #Write-Output $filename

    $outfile = "$filename.pdf" 
    & 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destloc $outfile" "$url"
    Start-Sleep -s 3
    #Write-Output $outfile

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文