Ruby on rails Rails 4:为什么生成的PDF中的表没有显示所有的行和单元格
我在Rails 4中有一个生成PDF的应用程序。这些PDF是包含表格和其他信息的报告。这些表格在应用程序的视图中动态生成,然后当用户按下按钮报告时,其将导出PDf。我的问题是表格看起来不完整。我需要显示该表上的所有信息,但只显示第一行。请看屏幕截图 从图中,我只看到第一行完成,但我还需要第二行或下一行带有记号。勾号会从应用程序中添加或删除,这意味着没有勾号的单元格是空的。 我试图从一段时间内解决这个问题,但运气不好 与本表相关的代码如下: 从控制器:Ruby on rails Rails 4:为什么生成的PDF中的表没有显示所有的行和单元格,ruby-on-rails,pdf,Ruby On Rails,Pdf,我在Rails 4中有一个生成PDF的应用程序。这些PDF是包含表格和其他信息的报告。这些表格在应用程序的视图中动态生成,然后当用户按下按钮报告时,其将导出PDf。我的问题是表格看起来不完整。我需要显示该表上的所有信息,但只显示第一行。请看屏幕截图 从图中,我只看到第一行完成,但我还需要第二行或下一行带有记号。勾号会从应用程序中添加或删除,这意味着没有勾号的单元格是空的。 我试图从一段时间内解决这个问题,但运气不好 与本表相关的代码如下: 从控制器: def export_acrf ht
def export_acrf
html = ""
html += "<br>"
html += params[:table_data].gsub('<span class="glyphicon glyphicon-ok text-success"></span>', '✔') if params[:table_data].present?
html += "</td></tr></tbody></table>"
html += PdfReport.page_break
study_version = StudyVersion.find(params[:id])
study = Study.find(study_version.study_id)
lock = Token.obtain("#{study.identifier}_ACRF", "", current_user)
forms = Form.where(study_version_id: study_version.id).order('ordinal asc')
forms.each do |form|
html += PdfReport.page_break
html += Mdr.form_to_html_annotations(form.form_id, form.form_namespace, true).html_safe
end
#pdf = PdfReport.create("Annotated CRF", "#{study.label}", current_user, html)
header_html = view_context.render 'shared/report.html.erb'
pdf = PdfReport.create("Case Report Form", "#{study.label}", current_user, html, header_html)
send_data pdf, filename: "#{study.identifier}_aCRF.pdf", type: 'application/pdf', disposition: 'inline'
lock.release
end
def导出\u acrf
html=“”
html+=“
”
html+=params[:table_data].gsub(“”,✔;')如果params[:table_data.),是否存在?
html+=“”
html+=PdfReport.page\u中断
研究版本=研究版本.find(参数[:id])
研究=研究.查找(研究版本.研究id)
lock=Token.get(“#{study.identifier}_ACRF”,当前_用户)
表单=表单。其中(研究版本号:研究版本号)。顺序('ordinal asc')
表格。每个都做表格|
html+=PdfReport.page\u中断
html+=Mdr.form\u到html\u注释(form.form\u id,form.form\u名称空间,true)。html\u安全
结束
#pdf=PdfReport.create(“带注释的CRF”,“#{study.label}”,当前_用户,html)
header\u html=view\u context.render“shared/report.html.erb”
pdf=PdfReport.create(“病例报告表”、“#{study.label}”、当前_用户、html、标题_html)
发送数据pdf,文件名:“#{study.identifier}_aCRF.pdf”,键入:“application/pdf”,处置:“inline”
开锁
结束
班级:
class PdfReport
C_CLASS_NAME = "PdfReport"
def self.create(title, sub_title, user, body_html, header_html)
paper_size = "A4"
html = page_header(header_html)
html += title_page(title, sub_title, user)
# Document history
html += body_html
# Completion instructions
html += page_footer()
ConsoleLogger.log(C_CLASS_NAME, "create", "HTML=" + html.to_s)
# pdf = WickedPdf.new.pdf_from_string(html, :page_size => paper_size, :footer => {:font_size => "10", :font_name => "Arial, \"Helvetica Neue\", Helvetica, sans-serif", :left => "", :center => "", :right => "[page] of [topage]"} )
options = { :page_size => paper_size, :footer => {:font_size => "10", :font_name => "Arial, \"Helvetica Neue\", Helvetica, sans-serif", :left => "", :center => "", :right => "[page] of [topage]"} }
pdf = CdiscChangesReport.export(html, options)
return pdf
end
def self.page_header(header_html)
html = "<html><head>"
# html += header_html
html += '<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">'
html += "<style>"
html += "h1 { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 24pt; line-height: 34pt; }\n"
html += "h1.title { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 24pt; line-height: 30pt; text-align: center; margin-top: 0; }\n"
html += "h2 { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 18pt; line-height: 28pt; }\n"
html += "h2.title { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 18pt; line-height: 24pt; text-align: center; margin-top: 0; }\n"
html += "h3 { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 16pt; }\n"
html += "h4 { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 14pt; }\n"
html += "h5 { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 12pt; }\n"
html += "p { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; }\n"
html += "table tr td { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; text-align: left; vertical-align: top; padding: 5px;}\n"
html += "table.simple { border: 1px solid black; border-collapse: collapse; width: 100%;}\n"
html += "table.simple tr td { border: 1px solid black; font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; text-align: left; vertical-align: top; padding: 5px;}\n"
html += "table.simple tr th { border: 1px solid black; font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; text-align: left; vertical-align: top; padding: 5px;}\n"
html += "table.form_table { border: 1px solid black; width: 100%;}\n"
html += "table.form_table tr td { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; text-align: left; vertical-align: top; padding: 5px;}\n"
html += "table.form_table h4 { vertical-align: middle;}\n"
html += "table.form_table td:first-child{ font: bold; }\n"
html += "table.form_repeat { border: 1px solid black; width: 100%;}\n"
html += "table.form_repeat th { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; text-align: left; vertical-align: top; }\n"
html += "table.form_repeat tr td { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; text-align: left; vertical-align: top;}\n"
html += "table.details tr td { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 11pt; text-align: left; vertical-align: top; padding: 1px; }\n"
html += "table.ci { border: 1px solid black; width: 100%; border-collapse: collapse;}\n"
html += "table.ci tr td { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; text-align: left; vertical-align: top; padding: 5px; border-bottom: 1pt solid black; }\n"
html += ".ci td table, .ci td table tbody, .ci td table td { border:none; }\n" # Stops inheritence into markdown
html += "table.note { border: 1px solid black; width: 100%;}\n"
html += "table.note tr td { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; font: bold; text-align: left; vertical-align: top; }\n"
html += "table.input_field { border-left: 1px solid black; border-right: 1px solid black; border-bottom: 1px solid black;}\n"
html += "table.input_field tr td { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 8pt; text-align: center; vertical-align: center; padding: 5px; }\n"
html += "table.input_field td:not(:last-child){border-right: 1px dashed}\n"
html += "table.cl_field tr td { font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif; font-size: 10pt; text-align: left; vertical-align: center; padding: 2px; }\n"
html += "table.cl_field td:first-child{ border: 1px solid black; }\n"
html += "tr.warning { background-color: #fcf8e3; }\n"
html += "</style>"
html += "</head><body>"
return html
end
def self.page_footer
html = "</body></html>"
return html
end
def self.title_page(title, sub_title, user)
name = ENV['organization_title']
image_file = ENV['organization_image_file']
dir = Rails.root.join("app", "assets", "images")
file = File.join(dir, image_file)
time_generated = Time.now
# Generate HTML
html = "<br><br><div style=\"text-align: center;\"><img src=\"#{file}\" style=\"height:75px;\"></div>"
html += "<h2 class=\"title\">#{name}</h2>"
html += "<br>" * 10
html += "<h1 class=\"title\">#{title}<br>#{sub_title}</h1>"
html += "<br>" * 29
html += "<table class=\"details\" align=\"right\"><tr><td>Run at:</td><td>#{time_generated.strftime("%Y-%b-%d, %H:%M:%S")}</td></tr><tr><td>Run by:</td><td>#{user.email}</td></tr></table>"
html += page_break
return html
end
def self.page_break
return "<p style='page-break-after:always;'></p>"
end
end
classpdfreport
C_CLASS_NAME=“PdfReport”
def self.create(标题、子标题、用户、正文html、标题html)
纸张尺寸=“A4”
html=页面\页眉(页眉\ html)
html+=标题页面(标题、子标题、用户)
#文件历史
html+=正文\u html
#完成说明
html+=页脚()
ConsoleLogger.log(C_类名称,“创建”,“HTML=“+HTML.to_s”)
#pdf=WickedPdf.new.pdf\u from\u string(html,:page\u size=>paper\u size,:footer=>{:font\u size=>“10”,:font\u name=>“Arial,\“Helvetica Neue\”,Helvetica,sans serif“,:left=>”,:center=>,:right=>“[page]的[page]”
选项={:页面大小=>纸张大小,:页脚=>{:字体大小=>“10”,:字体名称=>“Arial,\“Helvetica Neue\”,Helvetica,无衬线“,:left=>”,:center=>,:right=>“[topage]的[页面]”}
pdf=CdiscChangesReport.export(html,选项)
返回pdf
结束
def self.page_header(header_html)
html=“”
#html+=标题\u html
html+=''
html+=“”
html+=“h1{字体系列:Arial,\“Helvetica Neue\”,Helvetica,无衬线字体;字体大小:24pt;行高:34pt;}\n”
html+=“h1.title{font-family:Arial,\'Helvetica-Neue\”,Helvetica,无衬线;字体大小:24pt;线条高度:30pt;文本对齐:居中;页边距顶部:0;}\n”
html+=“h2{字体系列:Arial,\“Helvetica Neue\”,Helvetica,无衬线字体;字体大小:18pt;行高:28pt;}\n”
html+=“h2.title{font-family:Arial,\'Helvetica Neue\”,Helvetica,无衬线;字体大小:18pt;行高:24pt;文本对齐:中间;页边距顶部:0;}\n”
html+=“h3{字体系列:Arial,\“Helvetica Neue\”,Helvetica,无衬线;字体大小:16pt;}\n”
html+=“h4{字体系列:Arial,\'Helvetica Neue\”,Helvetica,无衬线;字体大小:14pt;}\n”
html+=“h5{字体系列:Arial,\'Helvetica Neue\”,Helvetica,无衬线;字体大小:12pt;}\n”
html+=“p{font-family:Arial,\'Helvetica Neue\”,Helvetica,无衬线;字体大小:10pt;}\n”
html+=“table tr td{font-family:Arial,\'Helvetica-Neue\”,Helvetica,sans-serif;字体大小:10pt;文本对齐:左侧;垂直对齐:顶部;填充:5px;}\n”
html+=“table.simple{边框:1px纯黑色;边框折叠:折叠;宽度:100%;}\n”
html+=“table.simple tr td{border:1px纯黑色;字体系列:Arial,\'Helvetica Neue\',Helvetica,无衬线;字体大小:10pt;文本对齐:左侧;垂直对齐:顶部;填充:5px;}\n”
html+=“table.simple tr th{border:1px纯黑色;字体系列:Arial,\'Helvetica Neue\',Helvetica,无衬线;字体大小:10pt;文本对齐:左侧;垂直对齐:顶部;填充:5px;}\n”
html+=“table.form_table{边框:1px纯黑色;宽度:100%;}\n”
html+=“table.form\u table tr td{font-family:Arial,\'Helvetica Neue\”,Helvetica,无衬线;字体大小:10pt;文本对齐:左;垂直对齐:顶部;填充:5px;}\n”
html+=“table.form_table h4{垂直对齐:中间;}\n”
html+=“table.form_table td:first child{font:bold;}\n”
html+=“table.form_repeat{边框:1px纯黑色;宽度:100%;}\n”
html+=“table.form\u重复{font-family:Arial,\'Helvetica Neue\”,Helvetica,无衬线;字体大小:10pt;文本对齐:左侧;垂直对齐:顶部;}\n”
html+=“table.form\u repeat tr td{font-family:Arial,\'Helvetica-Neue\”,Helvetica,sans-serif;字体大小:10pt;文本对齐:左;垂直对齐:上;}\n”
html+=“table.details tr td{font-family:Arial,\'Helvetica-Neue\',Helvetica,sans-serif;字体大小:11pt;文本对齐:左侧;垂直对齐:顶部;填充:1px;}\n”
html+=“table.ci{border:1px纯黑色;宽度:100%;边框折叠:折叠;}\n”
html+=“table.ci tr td{font-family:Arial,\'Helvetica-Neue\',Helvetica,sans-serif;字体大小:10pt;文本对齐:左侧;垂直对齐:顶部;填充:5px;边框底部:1pt纯黑色;}\n”
html+=“.ci td table、.ci td table tbody、.ci td table td{border:none;}\n”#停止对标记的继承
html+=“table.note{边框:1px纯黑色;宽度:100%;}\n”
html+=“table.note tr td{font-family:Arial,\“Helvetica Neue\”,Helvetica,无衬线;字体大小:10pt;
<%= form_tag( "/study_versions/export_acrf?format=pdf", id: "preview_form_acrf", method: :post, style: "display: none;") do %>
<input type="hidden" name="id" value="<%= params[:id] %>" >
<input type="hidden" name="table_data" value="<%= params[:table_data] %>" />
<input type="text" name="study_version[export_type]" value="acrf" >
<input type="submit" value="Submit" />
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<% end %>
// Study Version Editor: Annotated CRF
//
// Initialise
//
// @param [Integer] studyVersionId the id for the study version
// @return [void]
function SveAcrf(studyVersionId) {
this.progress = new SveProgress('#aCrfPb');
this.studyVersionId = studyVersionId;
this.html = []; // Array for resulting HTML
var _this = this;
// Button handlers
$('#study_acrf').click(function() {
$('#soa_table thead tr th, #soa_table tbody tr td').removeClass('success'); //success
_this.start();
});
$('#export_acrf').click(function() {
// $('#table_data').val($("<div />")
// .append($("#soa_table").clone()).html());
var table_data = $("<div />").append($("#soa_table").clone()).html()
// $('#preview_form').submit();
window.open('/study_versions/' + studyVersionId + '/export?study_version[export_type]=acrf;table_data='+table_data);
});
}
// Start the CRF build
//
// @return [void]
SveAcrf.prototype.start = function() {
var _this = this;
this.clear();
$.ajax({
url: "/study_versions/" + this.studyVersionId,
type: 'GET',
dataType: 'json',
success: function(result){
var forms = result.data.children;
if (forms.length > 0) {
_this.progress.clear(forms.length); // Set the progress total
var table = $("<div />").append($("#soa_table").clone()).html()
_this.html.push(table);
for (var i=0; i<forms.length; i++) {
_this.html.push(_this.placeholder(i, forms[i].label)); // Create the slot for the result.
_this.getForm(forms[i], i);
}
$("#aCrfHtml").append(_this.html.join(''));
} else {
displayWarning("Study does not include any forms at present, nothing to display.");
}
},
error: function(xhr,status,error){
handleAjaxError(xhr, status, error);
}
});
}
// Display form
//
// @param [JS Object] form structure containing the form id and namespace
// @param [Integer] index the index of the form
// @return [void]
SveAcrf.prototype.getForm = function(form, index) {
var _this = this;
$.ajax({
url: "/mdrs/form_annotations",
data: { "id": form.form_id, "namespace": form.form_namespace },
type: 'GET',
dataType: 'html',
success: function(result){
_this.displayForm(result, index);
setTimeout(function(){ $(".spinner_and_label_"+index).hide(); }, 500);
},
error: function(xhr,status,error){
handleAjaxError(xhr, status, error);
}
});
}
// Form display
//
// @param [result] the form html
// @param [Integer] index the index of the form
// @return [void]
SveAcrf.prototype.displayForm = function(result, index) {
this.html[index] = result; // Save the result in the correct slot
$("#aCrfHtml").append(this.html.join('')); // Joint the array to form the whole page.
this.progress.increment();
}
// Clear the CRF
//
// @return [void]
SveAcrf.prototype.clear = function() {
this.progress.clear(0);
this.html = [];
$("#aCrfHtml").html("");
}
// Placeholder html for building the CRF
//
// @param [String] label the form label
// @return [String] the html placeholder
SveAcrf.prototype.placeholder = function(index, label) { return '<div class="row spinner_and_label_'+index+'"><div class="col-md-3 col-sm-4"><p><i class="fa fa-refresh fa-spin fa-lg fa-fw margin-bottom"></i></p></div>' +
'<div class="col-md-9 col-sm-8"><p>Form: ' + label + ' will appear here ...</p></div></div>';
}
SveAcrf.prototype.openWindowWithPost = function (url, data) {
var form = document.createElement("form");
form.target = "_blank";
form.method = "POST";
form.action = url;
form.style.display = "none";
for (var key in data) {
var input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = data[key];
form.appendChild(input);
}
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
html += params[:table_data]
</thead><tbody><tr><td class=\"soa-row\" id=\"2\">Demographics (Pilot)</td><td class=\"soa-element text-center\" form_id=\"2\" visit_id=\"112\" id=\"36\"><span class=\"glyphicon glyphicon-ok text-success\"></span></td><td class=\"soa-element text-center\" form_id=\"2\" visit_id=\"113\" id=\"37\"><span class=\"glyphicon glyphicon-ok text-success\"></span></td><td class=\"soa-element text-center\" form_id=\"2\" visit_id=\"114\" id=\"43\"><span class=\"glyphicon glyphicon-ok text-success\"></span></td><td class=\"soa-element text-center\" form_id=\"2\" visit_id=\"115\" id=\"44\"><span class=\"glyphicon glyphicon-ok text-success\"></span></td><td class=\"soa-element text-center\" form_id=\"2\" visit_id=\"116\" id=\"45\"><span class=\"glyphicon glyphicon-ok text-success\"></span></td><td class=\"soa-element\" form_id=\"2\" visit_id=\"117\" id=\"0\">"