I have a very strange an unique issue in front of me. quick summary is I need to convert a word doc to a pdf. Easy enough right? Well the issue is that I need to convert a word doc to pdf while keeping the font as selectable text. Unfortunately the font I HAVE to use is myriad pro which is a mac font. This means that when I do the conversion to PDF with this method.
$path = 'C:\temp\Convert'
$wd = New-Object -ComObject Word.Application
Get-ChildItem -Path $path -Include *.doc, *.docx -Recurse |
ForEach-Object {
$doc = $wd.Documents.Open($_.Fullname)
$pdf = $_.FullName -replace $_.Extension, '.pdf'
$doc.ExportAsFixedFormat($pdf,17,$false,0,3,1,1,0,$false, $false,0,$false, $true)
$doc.Close()
}
$wd.Quit()
or
$word_app = New-Object -ComObject Word.Application
$word_app.visible = $False
##
## Get the files from the folder
##
$files = Get-ChildItem $Ftype
##
## process each file - open in WORD and save as PDF in output folder
## then delete original WORD file
##
write-host "Starting DOC Conversion"
ForEach ($file in $files) {
$d2 = $file.name
$Out = $OutPath + $d2 -replace ".doc", $NewSuffix
#$Out = $Out -replace '\s',''
$LogLine = "Converting from " + $file.fullname + " to " + $Out
$LogLine >>'C:\temp\WordPDF.LOG'
write-host $LogLine
$document = $word_app.Documents.Open($file.fullname)
#$document.ExportAsFixedFormat($Out,[Microsoft.Office.Interop.Word.WdExportOptimizeFor]::wdExportOptimizeForPrint)
$document.SaveAs($Out, 17)
$document.Close()
$Dcounter = $Dcounter + 1
$Wcounter = $Wcounter + 1
if ($Deletefiles) {
Remove-Item $file
write-host "Deleting " $file.fullname
}
}
$word_app.Quit()
so i've tried the "exportAsFixedFormat" method and the "SaveAs". Both work as far as conversion goes but it creates a bitmap pdf instead of selectable text.
The only way i've found that would solve my problem is to "print to PDF" and here's how i've approached it..
#===================================
#Get the default printer and save to a variable
$DefPrinter=Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true"
Write-Output $DefPrinter
#List all printers if you don't know the printer name
$xgetPrinters=Get-WmiObject -Query " SELECT * FROM Win32_Printer" | Select Name
Write-Output $xgetPrinters
#Set the default printer for printing while the code is executed
$Printer_net = New-Object -COM WScript.Network
$Printer_net.SetDefaultPrinter("Microsoft Print to PDF")
$xpath="C:\temp\ConvertDoc\"
$folders=get-childitem $xpath | select Name
$folders2=get-childitem $xpath
foreach($pdf_files in $folders)
{
$PDF_file_name = $pdf_files.Name;
Start-Process $xpath$PDF_file_name -Verb Print
}
This way creates a PDF with selectable text. Although there are two issues.
Setting the default printer doesn't actually work. I have to go into windows settings and set the default printer manually.
I can't figure out how to suppress the save as prompt and name the file the name of the current file it's trying to convert.
Can anyone help with this please. I am STUCK and out of ideas.