Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,22 @@ class LinuxInAppWebViewController extends PlatformInAppWebViewController
return await channel?.invokeMethod<Uint8List?>('takeScreenshot', args);
}

@override
Future<Uint8List?> createPdf({
@Deprecated("Use pdfConfiguration instead")
// ignore: deprecated_member_use_from_same_package
IOSWKPDFConfiguration? iosWKPdfConfiguration,
PDFConfiguration? pdfConfiguration,
}) async {
Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent(
'pdfConfiguration',
() => pdfConfiguration?.toMap() ?? iosWKPdfConfiguration?.toMap(),
);
// מחזיר null כשה-libWPEWebKit שרץ אינו מה-runtime המטולא של אוצריא.
return await channel?.invokeMethod<Uint8List?>('createPdf', args);
}

@override
Future<Uint8List?> saveState() async {
Map<String, dynamic> args = <String, dynamic>{};
Expand Down
62 changes: 62 additions & 0 deletions flutter_inappwebview_linux/linux/in_app_webview/in_app_webview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2473,6 +2473,68 @@ void InAppWebView::takeScreenshot(std::function<void(const std::optional<std::ve
callback(png_data);
}

// === PDF export (Otzaria WPE runtime patch) ===

namespace {

typedef void (*OtzariaPrintToPdfFn)(WebKitWebView*, double, double, double, double,
double, double, GCancellable*, GAsyncReadyCallback, gpointer);
typedef GBytes* (*OtzariaPrintToPdfFinishFn)(WebKitWebView*, GAsyncResult*, GError**);

struct CreatePdfCall {
std::function<void(const std::optional<std::vector<uint8_t>>&)> callback;
OtzariaPrintToPdfFinishFn finish;
};

} // namespace

void InAppWebView::createPdf(double pageWidthPt, double pageHeightPt,
double marginTopPt, double marginRightPt,
double marginBottomPt, double marginLeftPt,
std::function<void(const std::optional<std::vector<uint8_t>>&)> callback) {
if (webview_ == nullptr || callback == nullptr) {
if (callback) {
callback(std::nullopt);
}
return;
}

// הסימבולים קיימים רק ב-libWPEWebKit מה-runtime המטולא של אוצריא;
// בבנייה רגילה dlsym מחזיר null ואנחנו מדווחים "אין תמיכה" בשקט.
auto print_fn = reinterpret_cast<OtzariaPrintToPdfFn>(
dlsym(RTLD_DEFAULT, "webkit_otzaria_web_view_print_to_pdf"));
auto finish_fn = reinterpret_cast<OtzariaPrintToPdfFinishFn>(
dlsym(RTLD_DEFAULT, "webkit_otzaria_web_view_print_to_pdf_finish"));
if (print_fn == nullptr || finish_fn == nullptr) {
callback(std::nullopt);
return;
}

auto* call = new CreatePdfCall{std::move(callback), finish_fn};
print_fn(
webview_, pageWidthPt, pageHeightPt, marginTopPt, marginRightPt, marginBottomPt,
marginLeftPt, nullptr,
[](GObject* source, GAsyncResult* result, gpointer user_data) {
std::unique_ptr<CreatePdfCall> call(static_cast<CreatePdfCall*>(user_data));
GError* error = nullptr;
GBytes* bytes = call->finish(WEBKIT_WEB_VIEW(source), result, &error);
if (error != nullptr || bytes == nullptr) {
if (error != nullptr) {
g_warning("createPdf failed: %s", error->message);
g_error_free(error);
}
call->callback(std::nullopt);
return;
}
gsize size = 0;
const auto* data = static_cast<const uint8_t*>(g_bytes_get_data(bytes, &size));
std::vector<uint8_t> pdf(data, data + size);
g_bytes_unref(bytes);
call->callback(pdf);
},
call);
}

// === Session State ===

std::optional<std::vector<uint8_t>> InAppWebView::saveState() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ class InAppWebView {
// Screenshot - captures the current visible content as PNG data
void takeScreenshot(std::function<void(const std::optional<std::vector<uint8_t>>&)> callback);

// PDF export via the Otzaria WPE runtime patch (webkit_otzaria_web_view_print_to_pdf,
// resolved with dlsym). Sizes are in PDF points; yields nullopt when the
// running libWPEWebKit lacks the symbol (unpatched build).
void createPdf(double pageWidthPt, double pageHeightPt,
double marginTopPt, double marginRightPt,
double marginBottomPt, double marginLeftPt,
std::function<void(const std::optional<std::vector<uint8_t>>&)> callback);

// Session state - save and restore navigation state
std::optional<std::vector<uint8_t>> saveState() const;
bool restoreState(const std::vector<uint8_t>& stateData);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "webview_channel_delegate.h"

#include <cstring>
#include <utility>

#include "../in_app_browser/in_app_browser.h"
#include "../types/client_cert_challenge.h"
Expand Down Expand Up @@ -621,6 +622,62 @@ void WebViewChannelDelegate::HandleMethodCall(FlMethodCall* method_call) {
return;
}

if (string_equals(methodName, "createPdf")) {
// ברירת מחדל: A4 עם שוליים 1 ס"מ. pdfConfiguration.settings עוקף —
// אותם שדות ויחידות (אינצ'ים) כמו במימוש של Windows.
double pageWidthIn = 8.2677, pageHeightIn = 11.6929;
double marginTopIn = 0.3937, marginRightIn = 0.3937;
double marginBottomIn = 0.3937, marginLeftIn = 0.3937;
bool landscape = false;

auto asDouble = [](FlValue* v, double fallback) -> double {
if (v == nullptr) return fallback;
if (fl_value_get_type(v) == FL_VALUE_TYPE_FLOAT) return fl_value_get_float(v);
if (fl_value_get_type(v) == FL_VALUE_TYPE_INT) return static_cast<double>(fl_value_get_int(v));
return fallback;
};

if (args != nullptr && fl_value_get_type(args) == FL_VALUE_TYPE_MAP) {
FlValue* config = fl_value_lookup_string(args, "pdfConfiguration");
FlValue* settings = (config != nullptr && fl_value_get_type(config) == FL_VALUE_TYPE_MAP)
? fl_value_lookup_string(config, "settings")
: nullptr;
if (settings != nullptr && fl_value_get_type(settings) == FL_VALUE_TYPE_MAP) {
pageWidthIn = asDouble(fl_value_lookup_string(settings, "pageWidth"), pageWidthIn);
pageHeightIn = asDouble(fl_value_lookup_string(settings, "pageHeight"), pageHeightIn);
FlValue* orientation = fl_value_lookup_string(settings, "orientation");
if (orientation != nullptr && fl_value_get_type(orientation) == FL_VALUE_TYPE_INT) {
landscape = fl_value_get_int(orientation) == 1;
}
FlValue* margins = fl_value_lookup_string(settings, "margins");
if (margins != nullptr && fl_value_get_type(margins) == FL_VALUE_TYPE_MAP) {
marginTopIn = asDouble(fl_value_lookup_string(margins, "top"), marginTopIn);
marginRightIn = asDouble(fl_value_lookup_string(margins, "right"), marginRightIn);
marginBottomIn = asDouble(fl_value_lookup_string(margins, "bottom"), marginBottomIn);
marginLeftIn = asDouble(fl_value_lookup_string(margins, "left"), marginLeftIn);
}
}
}
if (landscape) {
std::swap(pageWidthIn, pageHeightIn);
}

g_object_ref(method_call);
webView->createPdf(
pageWidthIn * 72.0, pageHeightIn * 72.0, marginTopIn * 72.0, marginRightIn * 72.0,
marginBottomIn * 72.0, marginLeftIn * 72.0,
[method_call](const std::optional<std::vector<uint8_t>>& result) {
if (result.has_value() && !result->empty()) {
g_autoptr(FlValue) val = fl_value_new_uint8_list(result->data(), result->size());
fl_method_call_respond_success(method_call, val, nullptr);
} else {
fl_method_call_respond_success(method_call, nullptr, nullptr);
}
g_object_unref(method_call);
});
return;
}

if (string_equals(methodName, "getSelectedText")) {
// Capture method_call for async callback
g_object_ref(method_call);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3241,6 +3241,10 @@ abstract class PlatformInAppWebViewController extends PlatformInterface
apiUrl:
'https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2_16#printtopdfstream',
),
LinuxPlatform(
note:
'Requires the Otzaria-patched WPE runtime. With stock libWPEWebKit, this method returns null.',
),
],
)
Future<Uint8List?> createPdf({
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
import 'package:flutter_test/flutter_test.dart';

class _TestInAppWebViewController extends PlatformInAppWebViewController {
_TestInAppWebViewController()
: super.implementation(
const PlatformInAppWebViewControllerCreationParams(id: null),
);
}

void main() {
test('createPdf is advertised for the Linux implementation', () {
final controller = _TestInAppWebViewController();

expect(
controller.isMethodSupported(
PlatformInAppWebViewControllerMethod.createPdf,
platform: TargetPlatform.linux,
),
isTrue,
);
});
}
Loading