Plan sharing: "Print / Save as PDF" on the mobile app

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DBkhHLqWtSYrPdsNE11PiP
This commit is contained in:
HenryandClaude Sonnet 5 committed 2026-09-08 11:19:25 +01:00
1 parent acff80348f
commit 8e18e77437
7 files changed
+155 -7

No files matched your search

@@ -7,6 +7,7 @@ public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
registerPlugin(InstallPlugin.class);
registerPlugin(PrintPlugin.class);
super.onCreate(savedInstanceState);
}
}
@@ -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: '<!doctype 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 <style>, no assets).
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
printView = webView;
});
}
}