Skip to content

Instantly share code, notes, and snippets.

@mktakuya
Last active November 28, 2017 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mktakuya/2805a9833df0120352795bf78bc86dc0 to your computer and use it in GitHub Desktop.
Save mktakuya/2805a9833df0120352795bf78bc86dc0 to your computer and use it in GitHub Desktop.
苫小牧高専News FAX版のコード断片
require 'sinatra/base'
require 'sinatra/contrib'
require "sinatra/reloader"
require 'erb'
require 'open-uri'
require 'nokogiri'
require 'pdfkit'
class TmNCTNewsWeb < Sinatra::Base
get '/fax' do
path = "#{params[:category]}/#{params[:post_id]}.html"
url = "http://www.tomakomai-ct.ac.jp/#{path}"
doc = Nokogiri::HTML(open(url))
title = doc.title
post = doc.search('.post.clearfix')[0].to_html
erb = ERB.new(File.read(Dir.pwd + '/views/fax.html.erb'))
html = erb.result(binding)
pdf = PDFKit.new(html, encoding: 'UTF-8')
content_type 'application/pdf'
pdf.to_pdf
end
end
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title><%= title %></title>
</head>
<body>
<%= post %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment