Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39534
1
###############################################
2
# Printing an individual file to pdf
3
###############################################
4
5
async = require('async')
6
fs = require('fs')
7
temp = require('temp')
8
9
misc = require('smc-util/misc')
10
misc_node = require('smc-util-node/misc_node')
11
message = require('smc-util/message')
12
13
{defaults, required} = misc
14
15
print_sagews = (opts) ->
16
opts = defaults opts,
17
path : required
18
outfile : required
19
title : required
20
author : required
21
date : required
22
contents : required
23
subdir : required # 'true' or 'false', if true, then workdir is a generated subdirectory which will retain the temporary tex files
24
base_url : undefined # the base_url for downloading blobs/images
25
extra_data : undefined # extra data that is useful for displaying certain things in the worksheet.
26
timeout : 90
27
cb : required
28
29
extra_data_file = undefined
30
args = [opts.path, \
31
'--outfile', opts.outfile, \
32
'--title', opts.title, \
33
'--author', opts.author, \
34
'--date', opts.date, \
35
'--subdir', opts.subdir, \
36
'--contents', opts.contents\
37
]
38
if opts.base_url
39
args = args.concat(['--base_url', opts.base_url])
40
41
async.series([
42
(cb) ->
43
if not opts.extra_data?
44
cb(); return
45
extra_data_file = temp.path() + '.json'
46
args.push('--extra_data_file')
47
args.push(extra_data_file)
48
# NOTE: extra_data is a string that is *already* in JSON format.
49
fs.writeFile(extra_data_file, opts.extra_data, cb)
50
(cb) ->
51
# run the converter script
52
misc_node.execute_code
53
command : "smc-sagews2pdf"
54
args : args
55
err_on_exit : true
56
bash : false
57
timeout : opts.timeout
58
cb : cb
59
60
], (err) =>
61
if extra_data_file?
62
fs.unlink(extra_data_file) # no need to wait for completion before calling opts.cb
63
opts.cb(err)
64
)
65
66
exports.print_to_pdf = (socket, mesg) ->
67
ext = misc.filename_extension(mesg.path)
68
if ext
69
pdf = "#{mesg.path.slice(0,mesg.path.length-ext.length)}pdf"
70
else
71
pdf = mesg.path + '.pdf'
72
73
async.series([
74
(cb) ->
75
switch ext
76
when 'sagews'
77
print_sagews
78
path : mesg.path
79
outfile : pdf
80
title : mesg.options.title
81
author : mesg.options.author
82
date : mesg.options.date
83
contents : mesg.options.contents
84
subdir : mesg.options.subdir
85
extra_data : mesg.options.extra_data
86
timeout : mesg.options.timeout
87
cb : cb
88
else
89
cb("unable to print file of type '#{ext}'")
90
], (err) ->
91
if err
92
socket.write_mesg('json', message.error(id:mesg.id, error:err))
93
else
94
socket.write_mesg('json', message.printed_to_pdf(id:mesg.id, path:pdf))
95
)
96
97