我正试着用ruby写一个非常简单的类似markdown的转换器,然后将输出传递给PrinceXML (这太棒了)。普林斯基本上是把html转换成pdf。
下面是我的代码:
#!/usr/bin/ruby
# USAGE: command source-file.txt target-file.pdf
# read argument 1 as input
text = File.read(ARGV[0])
# wrap paragraphs in paragraph tags
text = text.gsub(/^(.+)/, '<p>\1</p>')
# create a new temp file for processing
htmlFile = File.new('/tmp/sample.html', "w+")
# place the transformed text in the new file
htmlFile.puts text
# run prince
system 'prince /tmp/sample.html #{ARGV[1]}'但是这会将一个空文件转储到/tmp/sample.html。当我排除调用普林斯时,转换发生得很好。
我做错了什么?
发布于 2011-06-04 12:10:33
由于您创建输出文件的方式,文件输出可能是缓冲的,而不是写入磁盘。试着这样做:
# create a new temp file for processing
File.open('/tmp/sample.html', "w+") do |htmlFile|
# place the transformed text in the new file
htmlFile.puts text
end
# run prince
system 'prince /tmp/sample.html #{ARGV[1]}'这是惯用的Ruby;我们将一个块传递给File.new,当该块退出时,它将自动关闭。作为关闭该文件的副产品,任何缓冲的输出都将被刷新到磁盘,您的system调用中的代码可以在其中找到它。
发布于 2011-06-04 11:06:30
从fine manual
王子doc.html -o out.pdf
将doc.html转换为out.pdf。
我认为你的system调用应该是这样的:
system "prince /tmp/sample.html -o #{ARGV[1]}"还要注意切换到双引号,以便#{}插值可以工作。如果没有双引号,shell将看到以下命令:
prince /tmp/sample.html #{ARGV[1]}然后,它将忽略#之后的所有内容作为注释。我不知道为什么你最终会得到一个空的/tmp/sample.html,根据我对文档的阅读,我希望得到一个/tmp/sample.pdf格式的文档。
https://stackoverflow.com/questions/6234315
复制相似问题