From 8e18e77437dfa55f8a425fc78bd2faafaa78c006 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 8 Sep 2026 11:19:25 +0100 Subject: [PATCH] Plan sharing: "Print / Save as PDF" on the mobile app MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The printable one-page plan layout (lib/plan-share.js planPrintHTML) was web-only: printPlan() renders it into a hidden iframe and calls window.print(), which Android's WebView does not implement. On mobile the same HTML is handed to the OS print flow via a small first-party `Print` Capacitor plugin (one method, printHtml), which is where the user picks "Save as PDF" / "Save to Files" or a real printer. Web is unchanged. - Android (PrintPlugin.java): load the HTML into an off-screen WebView and, once loaded, drive it through the system PrintManager. A strong ref keeps the WebView alive until the job spools. Registered in MainActivity next to InstallPlugin. - iOS (PrintPlugin.swift + .m): UIPrintInteractionController with a UIMarkupTextPrintFormatter. Files added to App.xcodeproj. - lib/mobile.js: printHtml() wrapper (registerPlugin('Print')). - sheets.jsx: the MOBILE branch of "Print / Save as PDF" calls printHtml; the web branch still calls printPlan. No new dependency — the OS renders the PDF. Native code is not exercised by the pipeline; run the manual APK job and check on device. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01DBkhHLqWtSYrPdsNE11PiP --- .../ch/duartesantos/opengym/MainActivity.java | 1 + .../ch/duartesantos/opengym/PrintPlugin.java | 67 +++++++++++++++++++ .../ios/App/App.xcodeproj/project.pbxproj | 8 +++ frontend/ios/App/App/PrintPlugin.m | 7 ++ frontend/ios/App/App/PrintPlugin.swift | 49 ++++++++++++++ frontend/src/lib/mobile.js | 12 ++++ frontend/src/sheets.jsx | 18 +++-- 7 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 frontend/android/app/src/main/java/ch/duartesantos/opengym/PrintPlugin.java create mode 100644 frontend/ios/App/App/PrintPlugin.m create mode 100644 frontend/ios/App/App/PrintPlugin.swift diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/MainActivity.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/MainActivity.java index 67cacdb..344028b 100644 --- a/frontend/android/app/src/main/java/ch/duartesantos/opengym/MainActivity.java +++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/MainActivity.java @@ -7,6 +7,7 @@ public class MainActivity extends BridgeActivity { @Override public void onCreate(Bundle savedInstanceState) { registerPlugin(InstallPlugin.class); + registerPlugin(PrintPlugin.class); super.onCreate(savedInstanceState); } } diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/PrintPlugin.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/PrintPlugin.java new file mode 100644 index 0000000..750d2f9 --- /dev/null +++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/PrintPlugin.java @@ -0,0 +1,67 @@ +package ch.duartesantos.opengym; + +import android.content.Context; +import android.print.PrintAttributes; +import android.print.PrintDocumentAdapter; +import android.print.PrintManager; +import android.webkit.WebView; +import android.webkit.WebViewClient; +import com.getcapacitor.Plugin; +import com.getcapacitor.PluginCall; +import com.getcapacitor.PluginMethod; +import com.getcapacitor.annotation.CapacitorPlugin; + +/** + * Minimal local Capacitor plugin that hands a self-contained HTML document to the Android + * system print flow (Save as PDF, Save to Drive, a real printer). Android WebView has no + * window.print(), so the printable plan page (lib/plan-share.js planPrintHTML) can only reach + * a PDF this way. No PDF library is bundled — the OS renders it. + * + * Usage from JS: + * import { registerPlugin } from '@capacitor/core'; + * const Print = registerPlugin('Print'); + * await Print.printHtml({ html: '…', name: 'Weekly Training Plan' }); + */ +@CapacitorPlugin(name = "Print") +public class PrintPlugin extends Plugin { + + // The print framework drives an off-screen WebView asynchronously; without a strong + // reference it can be collected mid-spool and the job dies silently. Held until the next + // print replaces it — one at a time is all the UI can start. + private WebView printView; + + @PluginMethod + public void printHtml(PluginCall call) { + String html = call.getString("html"); + if (html == null || html.isEmpty()) { + call.reject("html is required"); + return; + } + final String jobName = call.getString("name", "openGym"); + + getActivity().runOnUiThread(() -> { + WebView webView = new WebView(getContext()); + webView.setWebViewClient(new WebViewClient() { + @Override + public void onPageFinished(WebView view, String url) { + PrintManager printManager = + (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE); + if (printManager == null) { + printView = null; + call.reject("printing is not available on this device"); + return; + } + PrintDocumentAdapter adapter = view.createPrintDocumentAdapter(jobName); + printManager.print(jobName, adapter, + new PrintAttributes.Builder() + .setMediaSize(PrintAttributes.MediaSize.ISO_A4) + .build()); + call.resolve(); + } + }); + // baseURL null: the document is fully self-contained (inline