From 5584ad59c5f64ade257c1e156b400541ba46d17a Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sat, 7 Nov 2026 10:59:58 -0500 Subject: [PATCH] Printable logbook entries (PDF) for the paper logbook - Print one entry or several (tick them on the logbook list) as a PDF: Avery 5164/8164 or 5163/8163 adhesive labels (start at any label on a partly used sheet), or half-letter / letter pages - Each entry has the aircraft and serial, log, date, total time in service, the work, the signer's name and certificate, and a blank line to sign by hand; inspection entries with a recorded result get the 43.11(a)(5) statement unless the text already certifies - Labels: long entries shrink to fit, then continue on the next label. Pages: entries follow one another with their signature lines, numbered - Generated with Prawn (built-in fonts; unsupported characters become ?) Co-Authored-By: Claude Opus 5.5 --- Gemfile | 2 + Gemfile.lock | 17 +- README.md | 2 +- app/controllers/logbook_prints_controller.rb | 24 +++ app/services/logbook_entry_pdf.rb | 191 +++++++++++++++++++ app/views/logbook_entries/index.html.erb | 6 +- app/views/logbook_entries/show.html.erb | 5 +- app/views/logbook_prints/new.html.erb | 29 +++ config/routes.rb | 1 + test/integration/logbook_print_test.rb | 97 ++++++++++ 10 files changed, 366 insertions(+), 8 deletions(-) create mode 100644 app/controllers/logbook_prints_controller.rb create mode 100644 app/services/logbook_entry_pdf.rb create mode 100644 app/views/logbook_prints/new.html.erb create mode 100644 test/integration/logbook_print_test.rb diff --git a/Gemfile b/Gemfile index 5a370a0..b3f6f31 100644 --- a/Gemfile +++ b/Gemfile @@ -82,3 +82,5 @@ gem "commonmarker", "~> 2.10" # Markdown in notes gem "omniauth", "~> 2.1" # single sign-on (OpenID Connect) gem "omniauth_openid_connect", "~> 0.8" gem "omniauth-rails_csrf_protection", "~> 2.0" + +gem "prawn", "~> 2.5" # printable logbook entries diff --git a/Gemfile.lock b/Gemfile.lock index 800be3e..809041d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -92,7 +92,7 @@ GEM bcrypt_pbkdf (1.1.2) bcrypt_pbkdf (1.1.2-arm64-darwin) bcrypt_pbkdf (1.1.2-x86_64-darwin) - bigdecimal (4.1.3) + bigdecimal (3.3.1) bindata (3.0.1) bindex (0.8.1) bootsnap (1.26.1) @@ -274,6 +274,7 @@ GEM parser (3.3.13.0) ast (~> 2.4.1) racc + pdf-core (0.10.0) pdf-reader (2.16.0) Ascii85 (>= 1.0, < 3.0, != 2.0.0) afm (>= 0.2.1, < 2) @@ -281,6 +282,10 @@ GEM ttfunk pp (0.6.4) prettyprint + prawn (2.5.0) + matrix (~> 0.4) + pdf-core (~> 0.10.0) + ttfunk (~> 1.8) prettyprint (0.2.1) prism (1.9.0) propshaft (1.3.2) @@ -455,7 +460,8 @@ GEM thruster (0.1.27-x86_64-linux) timeout (0.6.1) tsort (0.2.0) - ttfunk (1.7.0) + ttfunk (1.8.0) + bigdecimal (~> 3.1) turbo-rails (2.0.23) actionpack (>= 7.1.0) railties (>= 7.1.0) @@ -519,6 +525,7 @@ DEPENDENCIES omniauth-rails_csrf_protection (~> 2.0) omniauth_openid_connect (~> 0.8) pdf-reader (~> 2.16) + prawn (~> 2.5) propshaft puma (>= 5.0) rails (~> 8.1.0) @@ -563,7 +570,7 @@ CHECKSUMS bcrypt_pbkdf (1.1.2) sha256=c2414c23ce66869b3eb9f643d6a3374d8322dfb5078125c82792304c10b94cf6 bcrypt_pbkdf (1.1.2-arm64-darwin) sha256=afdd6feb6ed5a97b8e44caacb3f2d641b98af78e6a516d4a3520b69af5cf9fea bcrypt_pbkdf (1.1.2-x86_64-darwin) sha256=35f5639d0058e6c2cc2f856f9c0b14080543268d3047abe6bc81c513093caa0e - bigdecimal (4.1.3) sha256=61ebe1e5e559bdc3cc6f2c0ee7f427321fc838f59611c294356eb04d6e21cf66 + bigdecimal (3.3.1) sha256=eaa01e228be54c4f9f53bf3cc34fe3d5e845c31963e7fcc5bedb05a4e7d52218 bindata (3.0.1) sha256=e97a826a12f6e7c5847e983e5826f277d61035317d352ec339b079537f2fa466 bindex (0.8.1) sha256=7b1ecc9dc539ed8bccfc8cb4d2732046227b09d6f37582ff12e50a5047ceb17e bootsnap (1.26.1) sha256=5b5bd90f8f13ea338627c1d56ffbe2ea25e743ac48c2afec1a9bea3a06a264e2 @@ -644,8 +651,10 @@ CHECKSUMS ostruct (0.6.3) sha256=95a2ed4a4bd1d190784e666b47b2d3f078e4a9efda2fccf18f84ddc6538ed912 parallel (2.3.0) sha256=f75a3e904101ce6a1ccb6b8dc800cbafd42166d83330192438dcf29395167a6f parser (3.3.13.0) sha256=e02f9b63c2fe05b0c68f692ba4df89b3cb113b663964f75d988fbebfd6c3deca + pdf-core (0.10.0) sha256=0a5d101e2063c01e3f941e1ee47cbb97f1adfc1395b58372f4f65f1300f3ce91 pdf-reader (2.16.0) sha256=bf1b5564c085264d8279bde3cf1467778b75841494d20c0fe7da3dad475f3732 pp (0.6.4) sha256=dfcb0fce700c41456265922884f9fe195d7fbb0674a3578e6c0f69588e82b570 + prawn (2.5.0) sha256=f4e20e3b4f30bf5b9ae37dad15eb421831594553aa930b2391b0fa0a99c43cb6 prettyprint (0.2.1) sha256=d555d57ea6d7cf001d4774b693546a6bd88bec1e778ddffb5c60b68ceec4b7d4 prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85 propshaft (1.3.2) sha256=1d56a3e56a92c21bfc29caf07406b5386b00d4c47ddf357cf989a5a234b1389e @@ -711,7 +720,7 @@ CHECKSUMS thruster (0.1.27-x86_64-linux) sha256=7e0e6381011401810b047ff25506e0e38755b2430499ff5586f1a110086981c6 timeout (0.6.1) sha256=78f57368a7e7bbadec56971f78a3f5ecbcfb59b7fcbb0a3ed6ddc08a5094accb tsort (0.2.0) sha256=9650a793f6859a43b6641671278f79cfead60ac714148aabe4e3f0060480089f - ttfunk (1.7.0) sha256=2370ba484b1891c70bdcafd3448cfd82a32dd794802d81d720a64c15d3ef2a96 + ttfunk (1.8.0) sha256=a7cbc7e489cc46e979dde04d34b5b9e4f5c8f1ee5fc6b1a7be39b829919d20ca turbo-rails (2.0.23) sha256=ee0d90733aafff056cf51ff11e803d65e43cae258cc55f6492020ec1f9f9315f tzinfo (2.0.6) sha256=8daf828cc77bcf7d63b0e3bdb6caa47e2272dcfaf4fbfe46f8c3a9df087a829b unicode-display_width (3.3.0) sha256=4b7aa66a4b11db50f6f7e98411215cd0dd10eaecb665d36eec5bcacb3fa0b613 diff --git a/README.md b/README.md index f39cb55..40053ae 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ To back up or move a single aircraft with all its files, use **Access & Sharing | New ADs | Weekly search of the Federal Register for newly published ADs mentioning the aircraft, engine or propeller models (or your own terms). Review each: track it as an AD or dismiss it. | | AD import | Import ADs from an FAA DRS export (`.xlsx` recommended: its AD numbers link to each AD's DRS page, which the import keeps; `.csv` works without links). ADs are matched by number, so nothing is duplicated. The preview suggests Track, Not applicable or Skip for each AD, and flags superseded or non-current ADs, model mismatches, appliance or STC-only ADs, and repetitive actions. | | Inspections | Shared inspection types with hour, month, and day recurrence, plus history. Each says what overdue means (grounded, no IFR, no transponder, advisory); an annual can also count as the 100-hour, and the 100-hour's 10-hour overrun is credited (91.409(b)). One click adds the standard Part 91 set. | -| Logbook | Maintenance entries with search and a link to a scanned document page. Records the signer's certificate kind and number and an inspection's result, and flags what 43.9 / 43.11 needs that's missing. Entries transcribed by Claude stay "unverified" until someone checks them against the scan. Templates for pilot preventive maintenance (43.3(g), Part 43 Appendix A(c)). | +| Logbook | Maintenance entries with search and a link to a scanned document page. Records the signer's certificate kind and number and an inspection's result, and flags what 43.9 / 43.11 needs that's missing. Entries transcribed by Claude stay "unverified" until someone checks them against the scan. Templates for pilot preventive maintenance (43.3(g), Part 43 Appendix A(c)). Print entries as a PDF on Avery 5163/5164 labels or half-letter/letter pages to sign and put in the paper logbook. | | Documents | Collections, multi-file documents with a page viewer, per-document share visibility, bulk upload, and expiration dates (registration, insurance…). | | Major repairs and alterations | Form 337, STC, and ICA tracking; an STC's ICA can become a tracked recurring inspection. | | Weight & balance | History of empty weight and CG (linked to the alteration that changed them) and a loading calculator: takeoff and landing weight and CG against max gross and the CG envelope. | diff --git a/app/controllers/logbook_prints_controller.rb b/app/controllers/logbook_prints_controller.rb new file mode 100644 index 0000000..26d4d5e --- /dev/null +++ b/app/controllers/logbook_prints_controller.rb @@ -0,0 +1,24 @@ +# Logbook entries as a PDF to print on labels or pages and put in the paper +# logbook (where they're signed by hand). +class LogbookPrintsController < ApplicationController + include AircraftScoped + before_action :set_entries + + def new + @layout = params[:layout].presence || "avery_5164" + end + + def show + pdf = LogbookEntryPdf.new(@aircraft, @entries, layout: params[:layout], start_at: params[:start_at]) + log_event("logbook", "Logbook entries printed (#{@entries.size})") + send_data pdf.render, filename: pdf.filename, type: "application/pdf", disposition: "inline" + end + + private + + def set_entries + ids = Array(params[:ids]).map(&:to_i).first(200) + @entries = @aircraft.logbook_entries.where(id: ids).includes(inspection_records: :inspection_type).order(:date, :id).to_a + redirect_to aircraft_logbook_entries_path(@aircraft), alert: "Choose the entries to print." if @entries.empty? + end +end diff --git a/app/services/logbook_entry_pdf.rb b/app/services/logbook_entry_pdf.rb new file mode 100644 index 0000000..fa108d6 --- /dev/null +++ b/app/services/logbook_entry_pdf.rb @@ -0,0 +1,191 @@ +require "prawn" +Prawn::Fonts::AFM.hide_m17n_warning = true # text is reduced to what the built-in fonts can show + +# Logbook entries as a PDF to print and put in the paper logbook: on +# adhesive labels or on pages. Each entry carries what 14 CFR 43.9 / 43.11 +# want (aircraft, date, time in service, the work, the signer's name and +# certificate) and a blank line for the signature, which must be signed by +# hand. On labels, long entries shrink to fit, then continue on the next +# label; on pages, entries follow one another, each with its signature line. +class LogbookEntryPdf + IN = 72 # points per inch + + Layout = Data.define(:key, :label, :page, :cols, :rows, :width, :height, :left, :top, :col_gap, :row_gap, :padding) do + def per_page = cols * rows + def labels? = per_page > 1 || page != [ width, height ] + end + + LAYOUTS = [ + Layout.new(key: "avery_5164", label: "Labels: Avery 5164 / 8164 (3⅓ × 4 in, 6 per sheet)", page: [ 8.5 * IN, 11 * IN ], + cols: 2, rows: 3, width: 4 * IN, height: 3.333 * IN, left: 0.156 * IN, top: 0.5 * IN, + col_gap: 0.188 * IN, row_gap: 0, padding: 0.15 * IN), + Layout.new(key: "avery_5163", label: "Labels: Avery 5163 / 8163 (2 × 4 in, 10 per sheet)", page: [ 8.5 * IN, 11 * IN ], + cols: 2, rows: 5, width: 4 * IN, height: 2 * IN, left: 0.156 * IN, top: 0.5 * IN, + col_gap: 0.188 * IN, row_gap: 0, padding: 0.12 * IN), + Layout.new(key: "half_letter", label: "Half-letter pages (5½ × 8½ in)", page: [ 5.5 * IN, 8.5 * IN ], + cols: 1, rows: 1, width: 5.5 * IN, height: 8.5 * IN, left: 0, top: 0, col_gap: 0, row_gap: 0, padding: 0.5 * IN), + Layout.new(key: "letter", label: "Letter pages (8½ × 11 in, also full-sheet labels)", page: [ 8.5 * IN, 11 * IN ], + cols: 1, rows: 1, width: 8.5 * IN, height: 11 * IN, left: 0, top: 0, col_gap: 0, row_gap: 0, padding: 0.75 * IN) + ].freeze + + def self.layout(key) = LAYOUTS.find { _1.key == key } || LAYOUTS.first + + def initialize(aircraft, entries, layout:, start_at: 1) + @aircraft = aircraft + @entries = entries + @layout = self.class.layout(layout) + @slot = (start_at.to_i - 1).clamp(0, @layout.per_page - 1) # skip used labels on the first sheet + end + + def filename = "#{@aircraft.tail_number.downcase}-logbook-entries.pdf" + + def render + info = { Title: "#{@aircraft.tail_number} logbook entries", Creator: "Simple Aircraft Manager" } + if @layout.labels? + @pdf = Prawn::Document.new(page_size: @layout.page, margin: 0, info: info) + @entries.each { |entry| place(entry) } + else + @pdf = Prawn::Document.new(page_size: @layout.page, margin: @layout.padding, info: info) + @entries.each_with_index { |entry, i| flow(entry, first: i.zero?) } + @pdf.number_pages clean("#{@aircraft.tail_number} · page of "), at: [ 0, -@layout.padding / 2 ], width: @pdf.bounds.width, + align: :right, size: 7, color: "777777" + end + @pdf.render + end + + private + + # Pages: the entry's lines, then its signature block (kept together), + # then a rule before the next one. + def flow(entry, first:) + small = 9 + w = @pdf.bounds.width + unless first + @pdf.move_down 10 + @pdf.stroke_color "BBBBBB" + @pdf.stroke_horizontal_rule + @pdf.move_down 12 + end + @pdf.start_new_page if @pdf.cursor < 90 # don't strand a heading at the foot of a page + @pdf.text clean(header_line), size: small, style: :bold + @pdf.text clean("#{LogbookEntry::LOG_TYPES[entry.log_type]} log · #{facts(entry).join(' ')}"), size: small + @pdf.move_down 4 + @pdf.text clean(body_text(entry)), size: 11, leading: 1.5 + @pdf.move_down 10 + height = signature_block_height(small) + 14 + @pdf.start_new_page if @pdf.cursor < height + @pdf.bounding_box([ 0, @pdf.cursor ], width: w, height: height) { signature_block(entry, w, height, small, more: false) } + end + + def header_line + [ @aircraft.tail_number, [ @aircraft.make, @aircraft.model ].compact_blank.join(" ").presence, + (@aircraft.serial_number.present? ? "S/N #{@aircraft.serial_number}" : nil) ].compact.join(" · ") + end + + def facts(entry) + [ "Date: #{entry.date&.strftime('%b %-d, %Y')}", + (entry.aircraft_hours_at_entry && "Total time: #{format('%.1f', entry.aircraft_hours_at_entry)} hrs (#{@aircraft.time_basis})") ].compact + end + + def place(entry) + remaining = body_text(entry) + continued = false + loop do + box = next_box + remaining = draw(entry, box, remaining, continued: continued) + break if remaining.blank? + continued = true + end + end + + # The next free label (or page): [x, y_top, width, height] inside the padding. + def next_box + if @slot >= @layout.per_page + @pdf.start_new_page + @slot = 0 + end + col = @slot % @layout.cols + row = @slot / @layout.cols + @slot += 1 + x = @layout.left + col * (@layout.width + @layout.col_gap) + @layout.padding + y = @layout.page[1] - @layout.top - row * (@layout.height + @layout.row_gap) - @layout.padding + [ x, y, @layout.width - 2 * @layout.padding, @layout.height - 2 * @layout.padding ] + end + + # Draws one label's worth; returns the body text that didn't fit. + def draw(entry, (x, y, w, h), body, continued:) + small = @layout.labels? ? 6.5 : 9 + base = @layout.labels? ? 8 : 11 + leftover = nil + @pdf.bounding_box([ x, y ], width: w, height: h) do + @pdf.text_box clean(header_line), at: [ 0, h ], width: w * 0.68, height: small + 2, size: small, style: :bold, overflow: :truncate + @pdf.text_box clean("#{LogbookEntry::LOG_TYPES[entry.log_type]} log#{' (continued)' if continued}"), + at: [ w * 0.68, h ], width: w * 0.32, height: small + 2, size: small, align: :right, overflow: :truncate + @pdf.text_box clean(facts(entry).join(" ")), at: [ 0, h - small - 3 ], width: w, height: small + 2, size: small, overflow: :truncate + + footer_h = signature_block_height(small) + body_top = h - 2 * (small + 3) - 2 + body_h = body_top - footer_h - 4 + leftover = @pdf.text_box(clean(body), at: [ 0, body_top ], width: w, height: body_h, size: base, min_font_size: [ base - 2.5, 5.5 ].max, + overflow: :shrink_to_fit, leading: 1) + signature_block(entry, w, footer_h, small, more: leftover.present?) + end + leftover + end + + def signature_block_height(small) = (small + 3) * 3 + 6 + + def signature_block(entry, w, height, small, more:) + @pdf.bounding_box([ 0, height ], width: w, height: height) do + if more + @pdf.text_box "Continued on the next label.", at: [ 0, height ], width: w, height: small + 2, + size: small, style: :italic, overflow: :truncate + end + line_y = height - (small + 3) * 1.6 + @pdf.stroke_color "555555" + @pdf.stroke_horizontal_line 0, w * 0.62, at: line_y + @pdf.text_box "Signature", at: [ 0, line_y - 1 ], width: w * 0.6, height: small, size: small - 1, overflow: :truncate + @pdf.text_box clean(certificate(entry)), at: [ w * 0.64, line_y + small + 1 ], width: w * 0.36, height: small * 2 + 4, + size: small, overflow: :shrink_to_fit, min_font_size: 5 + who = [ entry.signoff_person.presence || "Name: ____________________", entry.signoff_location.presence ].compact.join(" · ") + @pdf.text_box clean(who), at: [ 0, line_y - small - 1 ], width: w, height: small + 2, size: small, overflow: :truncate + end + end + + def certificate(entry) + kind = { "AP" => "A&P", "IA" => "A&P/IA", "A" => "A", "P" => "P", "REPAIR_STATION" => "Repair station", "REPAIRMAN" => "Repairman", + "PILOT" => "Pilot", "OTHER" => "" }[entry.signoff_certificate_type].to_s + number = entry.signoff_certificate_number.presence || "__________" + "#{kind.presence || 'Cert.'} No. #{number}" + end + + # The entry text, plus the 43.11(a)(5) statement for an inspection whose + # result is recorded but whose text doesn't already certify it. + def body_text(entry) + text = entry.text.to_s.strip + return text unless entry.entry_type == "INSPECTION" && entry.inspection_result.present? && !text.match?(/I certify/i) + type = inspection_kind(entry, text) + statement = + if entry.inspection_result == "approved" + "I certify that this aircraft has been inspected in accordance with #{type} inspection and was determined to be in airworthy condition." + else + "I certify that this aircraft has been inspected in accordance with #{type} inspection and a list of discrepancies and unairworthy items " \ + "dated #{entry.date&.strftime('%b %-d, %Y')} has been provided for the aircraft owner or operator." + end + "#{text}\n\n#{statement}" + end + + # "an annual" / "a 100-hour": from a linked inspection record, else the text. + def inspection_kind(entry, text) + name = entry.inspection_records.first&.inspection_type&.name.to_s.sub(/\s*\(.*\)\z/, "").sub(/\s*inspection\z/i, "").strip.downcase + name = "annual" if name.empty? && text.match?(/annual/i) + name = "100-hour" if name.empty? && text.match?(/100[- ]?h(ou)?r/i) + return "a ______________" if name.empty? + "#{name.match?(/\A[aeiou]/) ? 'an' : 'a'} #{name}" + end + + # The built-in PDF fonts cover Windows-1252; anything else becomes "?". + def clean(text) + text.to_s.unicode_normalize(:nfc).encode("Windows-1252", invalid: :replace, undef: :replace, replace: "?").encode("UTF-8") + end +end diff --git a/app/views/logbook_entries/index.html.erb b/app/views/logbook_entries/index.html.erb index 101e466..f8db638 100644 --- a/app/views/logbook_entries/index.html.erb +++ b/app/views/logbook_entries/index.html.erb @@ -12,17 +12,19 @@ <% end %> <%= f.submit "Filter", class: "btn-secondary" %> <% end %> + <% if @entries.any? %><% end %> <% if owner? %><%= link_to "Import scans", new_aircraft_logbook_import_path(@aircraft), class: "btn-secondary" %><%= link_to "+ New Entry", new_aircraft_logbook_entry_path(@aircraft), class: "btn-primary" %><% end %> <% if @entries.any? %> + <%= form_with url: new_aircraft_logbook_print_path(@aircraft), method: :get, id: "print-entries" do %><% end %>
- + <% @entries.each do |e| %> - + diff --git a/app/views/logbook_entries/show.html.erb b/app/views/logbook_entries/show.html.erb index 79b5e98..962a516 100644 --- a/app/views/logbook_entries/show.html.erb +++ b/app/views/logbook_entries/show.html.erb @@ -2,7 +2,10 @@

<%= LogbookEntry::LOG_TYPES[@entry.log_type] %> log — <%= fmt_date(@entry.date) %>

- <% if owner? %><%= link_to "Edit", edit_aircraft_logbook_entry_path(@aircraft, @entry), class: "btn-secondary btn-sm" %><% end %> +
+ <%= link_to "Print for logbook", new_aircraft_logbook_print_path(@aircraft, ids: [ @entry.id ]), class: "btn-secondary btn-sm" %> + <% if owner? %><%= link_to "Edit", edit_aircraft_logbook_entry_path(@aircraft, @entry), class: "btn-secondary btn-sm" %><% end %> +
<% unless @entry.verified? %> diff --git a/app/views/logbook_prints/new.html.erb b/app/views/logbook_prints/new.html.erb new file mode 100644 index 0000000..961c58a --- /dev/null +++ b/app/views/logbook_prints/new.html.erb @@ -0,0 +1,29 @@ +<% content_for :title, "Print logbook entries" %> +<%= render "shared/aircraft_header", aircraft: @aircraft %> +<%= form_with url: aircraft_logbook_print_path(@aircraft, format: :pdf), method: :get, class: "card card-body max-w-2xl space-y-4", + data: { turbo: false }, html: { target: "_blank" } do %> +
+

Print for the logbook

+

A PDF of <%= pluralize(@entries.size, "entry") %> to print on adhesive labels or paper and put in <%= @aircraft.tail_number %>'s logbook. Each one has a blank line to sign by hand: a printed entry isn't valid until it's signed.

+
+ <% @entries.each do |e| %><%= hidden_field_tag "ids[]", e.id, id: nil %><% end %> +
    + <% @entries.each do |e| %> +
  • <%= e.date %> · <%= LogbookEntry::LOG_TYPES[e.log_type] %> · <%= e.text.truncate(90) %> + <% if (missing = e.missing_record_items).any? %>(missing <%= missing.to_sentence %>)<% end %>
  • + <% end %> +
+
+ Paper + <% LogbookEntryPdf::LAYOUTS.each do |l| %> + + <% end %> +
+
+ <%= label_tag :start_at, "Start at label", class: "label" %> + <%= number_field_tag :start_at, 1, min: 1, max: 10, class: "input w-24" %> +

On a partly used label sheet, the first free label, counting across then down.

+
+

Print at 100% (“Actual size”), not “Fit to page”, so labels line up. Try a plain sheet first and hold it against the labels.

+
<%= submit_tag "Open PDF", class: "btn-primary" %><%= link_to "Back", aircraft_logbook_entries_path(@aircraft), class: "btn-secondary" %>
+<% end %> diff --git a/config/routes.rb b/config/routes.rb index 740468e..4d185d3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -60,6 +60,7 @@ Rails.application.routes.draw do collection { post :import } end resources :logbook_imports, path: "logbook/import", only: %i[new create show] + resource :logbook_print, path: "logbook/print", only: %i[new show] resources :logbook_entries do member { patch :verify } end diff --git a/test/integration/logbook_print_test.rb b/test/integration/logbook_print_test.rb new file mode 100644 index 0000000..3fe0404 --- /dev/null +++ b/test/integration/logbook_print_test.rb @@ -0,0 +1,97 @@ +require "test_helper" +require "pdf-reader" + +class LogbookPrintTest < ActionDispatch::IntegrationTest + setup do + @owner = create_user("owner@example.com") + @pilot = create_user("pilot@example.com") + @a = create_aircraft(owner: @owner, pilot: @pilot, tail_number: "N12345", make: "Cessna", model: "172N", serial_number: "17200001") + @entry = @a.logbook_entries.create!(date: Date.new(2026, 10, 1), log_type: "AC", entry_type: "MAINTENANCE", aircraft_hours_at_entry: 1234.5, + text: "Replaced left landing light lamp P/N 4509 — checked “OK”. ✈", signoff_person: "Sam Mechanic", + signoff_certificate_type: "AP", signoff_certificate_number: "7654321", signoff_location: "KOQU") + end + + def pdf(params) + get aircraft_logbook_print_path(@a, format: :pdf), params: params + assert_response :success + assert_equal "application/pdf", response.media_type + PDF::Reader.new(StringIO.new(response.body)) + end + + def text_of(reader) = reader.pages.map(&:text).join("\n").gsub(/\s+/, " ") + + test "an entry prints with what 43.9 wants and a line to sign" do + sign_in @pilot + get aircraft_logbook_entry_path(@a, @entry) + assert_select "a[href='#{new_aircraft_logbook_print_path(@a, ids: [ @entry.id ])}']", text: "Print for logbook" + get new_aircraft_logbook_print_path(@a, ids: [ @entry.id ]) + assert_select "input[type=radio][name=layout]", LogbookEntryPdf::LAYOUTS.size + assert_select "form[target=_blank][action='#{aircraft_logbook_print_path(@a, format: :pdf)}']" + + reader = pdf(ids: [ @entry.id ], layout: "avery_5164") + text = text_of(reader) + [ "N12345", "Cessna 172N", "S/N 17200001", "Aircraft log", "Date: Oct 1, 2026", "Total time: 1234.5 hrs (tach)", + "Replaced left landing light lamp P/N 4509 — checked “OK”. ?", "Signature", "Sam Mechanic", "KOQU", "A&P No. 7654321" ].each do |s| + assert_includes text, s + end + assert_equal [ 1, [ 612, 792 ] ], [ reader.page_count, reader.pages.first.attributes[:MediaBox].last(2).map(&:round) ] + assert_equal "Logbook entries printed (1)", @a.events.last.event_name + end + + test "inspection entries get the 43.11 statement unless they already certify" do + annual = InspectionType.create!(name: "Annual inspection (91.409(a))", recurring: true, recurring_months: 12).tap { _1.aircraft << @a } + inspection = @a.logbook_entries.create!(date: Date.new(2026, 9, 1), entry_type: "INSPECTION", text: "Performed annual per Part 43 App. D.", + inspection_result: "approved") + @a.inspection_records.create!(inspection_type: annual, date: inspection.date, logbook_entry: inspection) + found = @a.logbook_entries.create!(date: Date.new(2026, 9, 2), entry_type: "INSPECTION", text: "100 hour inspection.", inspection_result: "discrepancies") + already = @a.logbook_entries.create!(date: Date.new(2026, 9, 3), entry_type: "INSPECTION", inspection_result: "approved", + text: "I certify that this aircraft has been inspected in accordance with an annual inspection.") + sign_in @owner + text = text_of(pdf(ids: [ inspection.id, found.id, already.id ], layout: "letter")) + assert_includes text, "inspected in accordance with an annual inspection and was determined to be in airworthy condition" + assert_includes text, "in accordance with a 100-hour inspection and a list of discrepancies and unairworthy items dated Sep 2, 2026" + assert_equal 2, text.scan("I certify").size - 1 # the third already had its own + assert_includes text, "Cert. No. __________" # nothing recorded: blanks to fill by hand + end + + test "labels fill across then down, can start partway through a sheet, and long entries continue" do + entries = 7.times.map { |i| @a.logbook_entries.create!(date: Date.new(2026, 1, i + 1), text: "Entry #{i}") } + sign_in @owner + assert_equal 2, pdf(ids: entries.map(&:id), layout: "avery_5164").page_count # 6 per sheet + assert_equal 1, pdf(ids: entries.map(&:id), layout: "avery_5163").page_count # 10 per sheet + assert_equal 2, pdf(ids: entries.first(2).map(&:id), layout: "avery_5164", start_at: 6).page_count + assert_equal 2, pdf(ids: entries.map(&:id), layout: "half_letter").page_count # pages: entries follow one another + assert_equal [ 396, 612 ], pdf(ids: [ entries.first.id ], layout: "half_letter").pages.first.attributes[:MediaBox].last(2).map(&:round) + + long = @a.logbook_entries.create!(date: Date.current, text: (1..200).map { "Step #{_1} done." }.join(" ")) + text = text_of(pdf(ids: [ long.id ], layout: "avery_5163")) + assert_includes text, "Continued on the next label." + assert_includes text, "Aircraft log (continued)" + # Counted on single-column pages (side-by-side labels interleave when text is extracted). + longer = @a.logbook_entries.create!(date: Date.current, text: (1..3000).map { "w#{_1}" }.join(" ")) # words can't split across pages + reader = pdf(ids: [ longer.id ], layout: "half_letter") + assert_operator reader.page_count, :>, 1 + assert_includes text_of(reader), "N12345 · page 2 of #{reader.page_count}" + assert_equal (1..3000).to_a, text_of(reader).scan(/\bw(\d+)\b/).flatten.map(&:to_i) # in order, nothing lost + end + + test "pick several entries on the list; outsiders can't print" do + other = @a.logbook_entries.create!(date: Date.new(2026, 10, 2), text: "Second") + sign_in @owner + get aircraft_logbook_entries_path(@a) + assert_select "form#print-entries[action='#{new_aircraft_logbook_print_path(@a)}']" + assert_select "input[type=checkbox][name='ids[]'][form=print-entries]", 2 + get new_aircraft_logbook_print_path(@a), params: { ids: [ @entry.id, other.id ] } + assert_select "input[type=hidden][name='ids[]']", 2 + get new_aircraft_logbook_print_path(@a) + assert_redirected_to aircraft_logbook_entries_path(@a) + assert_equal "Choose the entries to print.", flash[:alert] + + elsewhere = create_aircraft(owner: create_user).logbook_entries.create!(date: Date.current, text: "Not yours") + assert_includes text_of(pdf(ids: [ @entry.id, elsewhere.id ], layout: "letter")).then { refute_includes _1, "Not yours"; _1 }, "Sam Mechanic" + + sign_in create_user + get aircraft_logbook_print_path(@a, format: :pdf), params: { ids: [ @entry.id ] } + assert_response :not_found + end +end
DateLogTypeHoursEntrySigned off
DateLogTypeHoursEntrySigned off
<%= link_to e.date, aircraft_logbook_entry_path(@aircraft, e), class: "link" %><%= link_to e.date, aircraft_logbook_entry_path(@aircraft, e), class: "link" %> <%= LogbookEntry::LOG_TYPES[e.log_type] %> <%= LogbookEntry::ENTRY_TYPES[e.entry_type] %> <%= hours(e.aircraft_hours_at_entry) %>