我想使用ruby创建一个脚本,该脚本将打印文件夹中所有的docx文件。 我想使用特殊的打印机设置来打印文件。需要从纸盘2打印第一页,然后从纸盘3打印第二页。
我尝试过的解决方案是这样的:
我使用pandoc gem将docx文件转换为pdf文件。
system("pandoc", "-o", "pandoc_tempAM.pdf", f) or raise "failed"
不幸的是,格式不正确。 有没有更好的方法可以将docx文件转换为pdf文件而不处理格式问题?
如果有人感兴趣,这里是整个脚本:
#!/usr/bin/ruby
require 'rubygems'
require 'combine_pdf'
require 'fileutils'
#Folgende gems müssen via Terminal (macOS) installiert werden bevor dieser Script benutzt werden kann.
#gem pristine --all (not required as I know so far)
#gem install combine_pdf
#gem install fileutils
#Because the first option isn't working, I decieded to create a thrid-party docx processor to convert the files to PDF
#brew install pandoc && brew cask install basictex
# export PATH=/Library/TeX/texbin:"$PATH"
#Search for DOCX Files in current Folder and loop though all Files
Dir.glob("/Users/test/spPrinting/TestFolder/**/*.docx").each do |f|
#Convert DOCX File to PDF
system("pandoc", "-o", "pandoc_tempAM.pdf", f) or raise "failed"
#Load the converted File in to Memory
pdf = CombinePDF.load("pandoc_tempAM.pdf")
#Opening a New PDF File
firstPdf = CombinePDF.new
#Extract the first Page from the PDF File in Memory. Index starts with ZERO!!
pdf_first_page = pdf.pages[0]
#Save the extracted Page into the newly opened PDF File
firstPdf << pdf_first_page
#Save the PDF File in to the FileSystem with the given Name
firstPdf.save "first_page_temp.pdf"
#Send the File to the Printer
#Use this cmd to find the printer options: lpoptions -p Test -l
system("lpr", "-P", "Test", "-o", "InputSlot=Tray3", "first_page_temp.pdf") or raise "lpr failed"
#Ruby'll sleep for 4 seconds.
sleep 4
#Opening a New PDF File
secPdf = CombinePDF.new
i = 0
#Extract all pages from the PDF file in memory except the first page. Index starts with ZERO!!
CombinePDF.load("pandoc_tempAM.pdf").pages.each do |page|
i += 1
secPdf << page if i > 1
end
#Save the PDF File in to the FileSystem with the given Name
secPdf.save "exceptFirstPage_temp.pdf"
#Send the File to the Printer
system("lpr", "-P", "Test", "-o", "InputSlot=Tray3", "exceptFirstPage_temp.pdf") or raise "lpr failed"
#Ruby'll sleep for 4 seconds.
sleep 4
FileUtils.rm("pandoc_tempAM.pdf")
FileUtils.rm("first_page_temp.pdf")
FileUtils.rm("exceptFirstPage_temp.pdf")
end
exit 1