Mr-RanX

我使用的是这两个 gem 包来生成 PDF(感觉有点不行,速度很慢)

gem \'wicked_pdf\'
gem \'wkhtmltopdf-binary\'

当有内容是 markdown,而且需要转换为 html 生成 PDF 时,我第一反应是在 js 里通过 marked 这个 npm 包把 markdown 的内容转换过后,通过 el.html()方法来替换,大致:

let el = $(\'.xxxxxxx\')
let markdownContent = el.data(\'content\') // 我把内容放在元素的 data 上
el.html(marked(markdownContent))

这样做的话,除了云上的图片都可以在 PDF 上完美展示

后面 google 了一会,发现了这个 gem kramdown,我们在 Gemfile 里添加gem \'kramdown\'之后,run bundle install并重启服务器,然后在合适的 helper 文件里添加:

def markdown_article_to_html(content = \'\')
  Kramdown::Document.new(content).to_html
end

然后在你 markdown 内容需要展示的地方写:

div.class-name == markdown_article_to_html(markdown_content)

注:我的 html文件是 slim 格式的,

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-12-14
  • 2021-04-09
  • 2022-02-12
  • 2021-06-06
  • 2022-12-23
  • 2021-08-04
猜你喜欢
  • 2021-12-06
  • 2022-02-07
  • 2021-11-17
  • 2022-12-23
相关资源
相似解决方案