使用phantomjs创建的PDF似乎使用ASCII

发布于 2025-02-08 12:12:38 字数 3440 浏览 1 评论 0 原文

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')

A am running an application in Meteor on Galaxy. I use phantomjs to generate a PDF-file from an HTML string. When I print the HTML-string in the console, all characters appear correct, but the rendered PDF shows Chinese characters as if it were using ASCII encoding. Examples: "骆高" → "éªé«" and "TÜV" → "TÃV".

Logs of the data/html looks fine here:

  • generatePdf method
  • @html in constructor
  • child.stdin.write args

I tried the following, to no avail:

  • Added <meta charset="utf-8" /> to the HTML
  • Added <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  • Set encoding for phantomjs: phantom.outputEncoding = 'utf8
  • Set encoding for child_process: child.stdin.setEncoding('utf8')
  • Set encoding for child_process: child.stdout.setEncoding('utf8')
  • Set encoding when writing file to S3 Bucket: Body: new Buffer buffer, 'utf8'

Versions used:

  • Meteor: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文