Forum How do I...?

Create PDF from the output of the control mode

msayed
I'm getting familiar with the control mode by starting a job and passing a JSON input. Afterward, I copy the generated PDF output, paste it into a text editor (like VS Code), and save it with a .pdf extension. However, the resulting file contains only blank pages.


Here's how I run the control command:

prince --control \                                                                                                                                                 
    --license-file prince.dat --user-agent 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'  \
    --no-warn-css


I pass the following json as the job data:

job BYTESIZE
{"input": { "src": "path/to/transcripts.html" }, "job-resource-count": 0}


I get the BYTESIZE using this command, then subtract 1 from the output:

echo '{"input": { "src": "path/to/transcripts.html" }, "job-resource-count": 0}' | wc -lc 


I used the same command without the control mode and the PDF is generated as expected.

prince "transcripts.html" -o "transcripts.pdf"  --user-agent 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36' --no-warn-css
msayed
I could not delete the question, but I managed to get it working by wrapping it in a ruby code:


require 'open3'


job = '{"input": { "src": "/Users/msayed/workspace/accredible/pdf/transcripts.html" }, "job-resource-count": 0}'
command = "prince --control --no-warn-css"

Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
  sleep 1
  begin
    while true do
      file.write stdout.read_nonblock(5)
    end
  rescue
  end

  stdin.puts "job #{job.bytesize}"
  stdin.puts "#{job}"
  sleep 1

  stdout.binmode
  # ignore pdf 72594
  stdout.read_nonblock(9)

  File.open("output.pdf", "wb") do |file|
    begin
      while true do
        output = stdout.read_nonblock(5)
        break if output.start_with? "log"
        file.write output
      end
    rescue
    end

    file.close
  end

  stdin.close
  stdout.close
  stderr.close
  wait_thr.join
end