Merge pull request #55 from sharelatex/bg-check-pdf-output-is-optimised
use pdfinfo on output to ensure pdfs are optimised
This commit is contained in:
@@ -28,6 +28,18 @@ compare = (originalPath, generatedPath, callback = (error, same) ->) ->
|
|||||||
console.log "compare result", stderr
|
console.log "compare result", stderr
|
||||||
callback null, false
|
callback null, false
|
||||||
|
|
||||||
|
checkPdfInfo = (pdfPath, callback = (error, output) ->) ->
|
||||||
|
proc = ChildProcess.exec "pdfinfo #{fixturePath(pdfPath)}"
|
||||||
|
stdout = ""
|
||||||
|
proc.stdout.on "data", (chunk) -> stdout += chunk
|
||||||
|
proc.stderr.on "data", (chunk) -> console.log "STDERR", chunk.toString()
|
||||||
|
proc.on "exit", () ->
|
||||||
|
if stdout.match(/Optimized:\s+yes/)
|
||||||
|
callback null, true
|
||||||
|
else
|
||||||
|
console.log "pdfinfo result", stdout
|
||||||
|
callback null, false
|
||||||
|
|
||||||
compareMultiplePages = (project_id, callback = (error) ->) ->
|
compareMultiplePages = (project_id, callback = (error) ->) ->
|
||||||
compareNext = (page_no, callback) ->
|
compareNext = (page_no, callback) ->
|
||||||
path = "tmp/#{project_id}-source-#{page_no}.png"
|
path = "tmp/#{project_id}-source-#{page_no}.png"
|
||||||
@@ -41,24 +53,30 @@ compareMultiplePages = (project_id, callback = (error) ->) ->
|
|||||||
compareNext page_no + 1, callback
|
compareNext page_no + 1, callback
|
||||||
compareNext 0, callback
|
compareNext 0, callback
|
||||||
|
|
||||||
|
comparePdf = (project_id, example_dir, callback = (error) ->) ->
|
||||||
|
convertToPng "tmp/#{project_id}.pdf", "tmp/#{project_id}-generated.png", (error) =>
|
||||||
|
throw error if error?
|
||||||
|
convertToPng "examples/#{example_dir}/output.pdf", "tmp/#{project_id}-source.png", (error) =>
|
||||||
|
throw error if error?
|
||||||
|
fs.stat fixturePath("tmp/#{project_id}-source-0.png"), (error, stat) =>
|
||||||
|
if error?
|
||||||
|
compare "tmp/#{project_id}-source.png", "tmp/#{project_id}-generated.png", (error, same) =>
|
||||||
|
throw error if error?
|
||||||
|
same.should.equal true
|
||||||
|
callback()
|
||||||
|
else
|
||||||
|
compareMultiplePages project_id, (error) ->
|
||||||
|
throw error if error?
|
||||||
|
callback()
|
||||||
|
|
||||||
downloadAndComparePdf = (project_id, example_dir, url, callback = (error) ->) ->
|
downloadAndComparePdf = (project_id, example_dir, url, callback = (error) ->) ->
|
||||||
writeStream = fs.createWriteStream(fixturePath("tmp/#{project_id}.pdf"))
|
writeStream = fs.createWriteStream(fixturePath("tmp/#{project_id}.pdf"))
|
||||||
request.get(url).pipe(writeStream)
|
request.get(url).pipe(writeStream)
|
||||||
writeStream.on "close", () =>
|
writeStream.on "close", () =>
|
||||||
convertToPng "tmp/#{project_id}.pdf", "tmp/#{project_id}-generated.png", (error) =>
|
checkPdfInfo "tmp/#{project_id}.pdf", (error, optimised) =>
|
||||||
throw error if error?
|
throw error if error?
|
||||||
convertToPng "examples/#{example_dir}/output.pdf", "tmp/#{project_id}-source.png", (error) =>
|
optimised.should.equal true
|
||||||
throw error if error?
|
comparePdf project_id, example_dir, callback
|
||||||
fs.stat fixturePath("tmp/#{project_id}-source-0.png"), (error, stat) =>
|
|
||||||
if error?
|
|
||||||
compare "tmp/#{project_id}-source.png", "tmp/#{project_id}-generated.png", (error, same) =>
|
|
||||||
throw error if error?
|
|
||||||
same.should.equal true
|
|
||||||
callback()
|
|
||||||
else
|
|
||||||
compareMultiplePages project_id, (error) ->
|
|
||||||
throw error if error?
|
|
||||||
callback()
|
|
||||||
|
|
||||||
Client.runServer(4242, fixturePath("examples"))
|
Client.runServer(4242, fixturePath("examples"))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user