$API_KEY = "xxxxxxxxxx"
# Source image files
$ImageFiles = (Get-ChildItem -Path C:UserssamDesktopjpeg* -filter *).Name
$uploadedFiles = @()
try {
foreach ($imageFile in $ImageFiles ) {
# 1a. RETRIEVE THE PRESIGNED URL TO UPLOAD THE FILE.
# Prepare URL for `Get Presigned URL` API call
$query = "https://api.pdf.co/v1/file/upload/get-presigned-url?
contenttype=application/octet-stream&name=" + `
[IO.Path]::GetFileName($imageFile)
$query = [System.Uri]::EscapeUriString($query)
# Execute request
$jsonResponse = Invoke-RestMethod -Method Get -Headers @{ "x-api-key" = $API_KEY } -Uri
$query
if ($jsonResponse.error -eq $false) {
# Get URL to use for the file upload
$uploadUrl = $jsonResponse.presignedUrl
# Get URL of uploaded file to use with later API calls
$uploadedFileUrl = $jsonResponse.url
# 1b. UPLOAD THE FILE TO CLOUD.
$r = Invoke-WebRequest -Method Put -Headers @{ "x-api-key" = $API_KEY; "content-type"
= "application/octet-stream" } -InFile $imageFile -Uri $uploadUrl
if ($r.StatusCode -eq 200) {
# Keep uploaded file URL
$uploadedFiles += $uploadedFileUrl
}
else {
# Display request error status
Write-Host $r.StatusCode + " " + $r.StatusDescription
}
}
else {
# Display service reported error
Write-Host $jsonResponse.message
}
}
if ($uploadedFiles.length -gt 0) {
# 2. CREATE PDF DOCUMENT FROM UPLOADED IMAGE FILES
# Prepare URL for `DOC To PDF` API call
$query = "https://api.pdf.co/v1/pdf/convert/from/image"
# Prepare request body (will be auto-converted to JSON by Invoke-RestMethod)
# See documentation: https://apidocs.pdf.co
$body = @{
"name" = $(Split-Path $DestinationFile -Leaf)
"url" = $uploadedFiles -join ","
} | ConvertTo-Json
# Execute request
$response = Invoke-WebRequest -Method Post -Headers @{ "x-api-key" = $API_KEY; "Content-
Type" = "application/json" } -Body $body -Uri $query
$jsonResponse = $response.Content | ConvertFrom-Json
if ($jsonResponse.error -eq $false) {
# Get URL of generated PDF file
$resultFileUrl = $jsonResponse.url;
$DestinationFile = "C:UserssamDesktoppdf$imagefile.split('.')[0]"
# Download PDF file
Invoke-WebRequest -Headers @{ "x-api-key" = $API_KEY } -OutFile $DestinationFile -Uri
$resultFileUrl
Write-Host "Generated PDF file saved as `"$($DestinationFile)`" file."
}
else {
# Display service reported error
Write-Host $jsonResponse.message
}
}
}
catch {
# Display request error
Write-Host $_.Exception
}
PS C:Usersnikhileshwar.yerragu> C:Usersnikhileshwar.yerraguDesktopPowerShell ScriptsJPEGTOPDF.ps1
System.Management.Automation.ItemNotFoundException:找不到路径"C:\Users\nikhileshwa"r.yerragu\arse-vivcharyk-dmRB6RXRpFk-unsplash.jpg',因为它不存在。位于System.Management.Automation.LocationGlobber.ExpandMshGlobPath(字符串路径,布尔allowNonexistingPaths,PSDriveInfo驱动器,ContainerCmdletProvider提供程序,CmdletProvider上下文(位于System.Management.Automation.LocationGlobber.ResolveDriveQualifiedPath(字符串路径,CmdletProviderContext上下文,布尔允许不存在路径,CmdletProvider&providerInstance(在System.Management.Automation.LocationGlobber.GetGlobbedMonadPathsFromMonadPath(String路径,布尔允许不存在路径,CmdletProviderContext上下文,CmdletProvider&providerInstance(在System.Management.Automation.LocationGlobber.GetGlobbedProviderPathsFromMonadPath(St环形路径,布尔允许不存在路径,CmdletProviderContext上下文,ProviderInfo&专业提供程序CmdletProvider&提供者实例(在System.Management.Automation.LocationGlobber.GetGlobbedProviderPathsFromMonadPath(St环形路径,布尔allowNonexistingPaths,ProviderInfo&提供者,CmdletProvider&提供者实例(位于System.Management.Automation.PSCmdlet.GetResolvedProviderPathFromPSPath(字符串路径,提供者信息&提供者(位于Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ValideParameters((
tl;dr
替换:
$ImageFiles = (Get-ChildItem -Path C:UserssamDesktopjpeg* -filter *).Name
带有:
$ImageFiles = (Get-ChildItem -Path C:UserssamDesktopjpeg* -filter *).FullName
可以简化为:
$ImageFiles = (Get-ChildItem -File -LiteralPath C:UserssamDesktopjpeg).FullName
错误消息不言而喻地告诉您文件C:Usersnikhileshwa r.yerraguarsen-vivcharyk-dmRB6RXRpFk-unsplash.jpg
不存在,因此需要从这里开始故障排除。
源是$imageFile
,它被用作迭代变量,以使用foreach
语句对存储在数组$ImageFiles
中的元素进行循环。
看看$ImageFile
是如何填充的——(Get-ChildItem -Path C:UserssamDesktopjpeg* -filter *).Name
——我们可以得出结论,它包含一个没有路径信息的文件名称、的数组。
这意味着,当您将-InFile $imageFile
传递给Invoke-WebRequest
调用时,只有当当前位置(目录(恰好是C:UserssamDesktopjpeg
时,它才会成功。
不依赖当前位置,而是使用完整路径填充$ImageFiles
阵列,如顶部所示。