使用phantomjs创建的PDF似乎使用ASCII
A正在在Galaxy的流星中运行一个应用程序。我使用phantomjs从HTML字符串生成PDF文件。当我将HTML弦打印在控制台中时,所有字符看起来都正确,但是渲染的PDF显示了汉字,好像它在使用ASCII编码一样。示例:“骆高”→“éªé«”
和“tüv”→“tãv”
。
数据/html的日志在这里看起来不错:
- generatepdf方法
- @html constructor
- child.stdin.write args
我尝试了以下内容,无用:
- 添加
< meta charset =“ utf-8”/> 添加到html
- 添加
< meta http-equiv =“ content-type” content =“ text/html; charset = utf-8”/>
- set for phantomjs:
phantom 。
-
- /code>
- 在将文件写入S3桶时进行编码:
主体:新的缓冲缓冲区,'utf8'
使用的版本:
- 流星:1.10.2
- Phantomjs:1.9.8
methods.coffee
Meteor.methods
generatePdf: () ->
# gather data from database
data.html = SSR.render('pdfTemplate', data)
Meteor.wrapAsync(createPdf)(data)
createPdf = (data,callback) ->
pdf = new PDF data.html, data.options
pdf.toFile (err, output) ->
if output
body = fs.createReadStream(output.filepath).pipe(zlib.createGzip())
fs.readFile output.filepath, (err,buffer) ->
return callback(err) if err
fs.unlink output.filepath, (err) ->
return callback(err) if err
s3.putObject
Bucket: "<Bucket>"
Key: "<Key>"
Body: new Buffer buffer
ContentType: 'application/pdf'
pdf.coffee
fs = Npm.require('fs')
Stream = Npm.require('stream').Readable
childprocess = Npm.require('child_process')
path = Npm.require('path')
assert = Npm.require('assert')
phantomjs = Npm.require('phantomjs')
class PDF
constructor: (@html, @options={}) ->
pageScript = @options.script or 'pdf_a4_portrait.js'
@script = path.join(Meteor.rootPath, 'assets/packages/fta_phantomjs/scripts', pageScript)
@options.directory = process.env.TMPDIR
@options.phantomArgs ?= []
assert(typeof @html is 'string' && @html.length, "html-pdf: Can't create a pdf without an html string")
@options.timeout = parseInt(@options.timeout) || 30000
toFile: (callback) ->
child = childprocess.spawn('phantomjs', [].concat(@options.phantomArgs, [@script]))
stdout = []
stderr = []
timeout = setTimeout ->
child.stdin.end()
child.kill()
stderr = [new Buffer('html-pdf: PDF generation timeout. Phantom.js script did not exit.')] unless stderr.length
, @options.timeout
child.stdout.on 'data', (buffer) ->
stdout.push(buffer)
child.stderr.on 'data', (buffer) ->
stderr.push(buffer)
child.stdin.end()
child.kill()
child.on 'exit', (code) ->
clearTimeout(timeout)
if code || stderr.length
err = new Error(Buffer.concat(stderr).toString() or 'html-pdf: Unknown Error')
return callback(err)
else
try
data = Buffer.concat(stdout).toString()?.trim()
data = JSON.parse(data)
return callback(null, data)
catch err
return callback(err)
args = JSON.stringify({@html, @options})+'\n'
child.stdin.write(args, 'utf8')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论