diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zig index 10bd28b3..e0408850 100644 --- a/src/cdp/domains/page.zig +++ b/src/cdp/domains/page.zig @@ -21,6 +21,7 @@ const std = @import("std"); const lp = @import("lightpanda"); const screenshot_png = @embedFile("screenshot.png"); +const screenshot_pdf = @embedFile("screenshot.pdf"); const id = @import("../id.zig"); const CDP = @import("../CDP.zig"); @@ -47,6 +48,7 @@ pub fn processMessage(cmd: *CDP.Command) !void { stopLoading, close, captureScreenshot, + printToPDF, getLayoutMetrics, handleJavaScriptDialog, }, cmd.input.action) orelse return error.UnknownMethod; @@ -63,6 +65,7 @@ pub fn processMessage(cmd: *CDP.Command) !void { .stopLoading => return cmd.sendResult(null, .{}), .close => return close(cmd), .captureScreenshot => return captureScreenshot(cmd), + .printToPDF => return printToPDF(cmd), .getLayoutMetrics => return getLayoutMetrics(cmd), .handleJavaScriptDialog => return handleJavaScriptDialog(cmd), } @@ -719,6 +722,7 @@ fn base64Encode(comptime input: []const u8) [std.base64.standard.Encoder.calcSiz return buf; } +// Return a fake screenshot fn captureScreenshot(cmd: *CDP.Command) !void { const Params = struct { format: ?[]const u8 = "png", @@ -755,6 +759,14 @@ fn captureScreenshot(cmd: *CDP.Command) !void { }, .{}); } +// Return a fake pdf +fn printToPDF(cmd: *CDP.Command) !void { + // Ignore all parameters. + return cmd.sendResult(.{ + .data = base64Encode(screenshot_pdf), + }, .{}); +} + fn getLayoutMetrics(cmd: *CDP.Command) !void { const width = 1920; const height = 1080; @@ -894,6 +906,21 @@ test "cdp.page: captureScreenshot" { } } +test "cdp.page: printToPDF" { + const LogFilter = @import("../../testing.zig").LogFilter; + const filter: LogFilter = .init(&.{.not_implemented}); + defer filter.deinit(); + + var ctx = try testing.context(); + defer ctx.deinit(); + { + try ctx.processMessage(.{ .id = 10, .method = "Page.printToPDF", .params = .{ .landscape = true } }); + try ctx.expectSentResult(.{ + .data = base64Encode(screenshot_pdf), + }, .{ .id = 10 }); + } +} + test "cdp.page: getLayoutMetrics" { var ctx = try testing.context(); defer ctx.deinit(); diff --git a/src/cdp/domains/screenshot.pdf b/src/cdp/domains/screenshot.pdf new file mode 100644 index 00000000..c61fecbf --- /dev/null +++ b/src/cdp/domains/screenshot.pdf @@ -0,0 +1,82 @@ +%PDF-1.4 +%âãÏÓ +1 0 obj +<< /Type /Catalog /Pages 2 0 R >> +endobj +2 0 obj +<< /Type /Pages /Kids [3 0 R] /Count 1 >> +endobj +3 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 1920 1080] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >> +endobj +4 0 obj +<< /Length 6030 >> +stream +q +4.5 0 0 -4.5 775 880 cm +0 0 0 rg +59.8175 53.1278 m 63.226 55.6474 66.8494 59.8264 68.5383 62.2538 c 68.9682 62.8684 68.4462 63.6673 67.7092 63.5444 c 61.6906 62.3768 41.6696 58.1364 24.6885 49.8092 c 24.32 49.6248 24.1358 49.2254 24.2586 48.8567 c 24.6271 47.5661 25.9168 44.9235 30.093 44.7392 c 30.7378 44.7084 31.1063 44.0017 30.7685 43.4793 c 29.878 42.0966 28.5269 39.9457 28.0049 38.2557 c 27.9435 38.0406 27.7899 37.8869 27.575 37.7947 c 26.7766 37.3953 24.4122 36.3506 19.9596 34.9985 c 14.3095 33.2778 12.1293 33.3393 11.0546 31.5878 c 9.9798 29.8363 9.9491 27.9927 12.4978 26.5485 c 15.0465 25.1043 25.9782 22.7075 29.7245 22.8612 c 29.7245 22.8612 30.9221 22.9226 30.8607 22.2159 c 30.7992 21.5092 30.2465 20.3415 30.2158 18.4364 c 30.1851 16.3162 32.5803 14.9642 34.7298 14.2882 c 36.7871 13.6736 39.3051 14.9027 41.1475 15.8246 c 41.3318 15.9168 41.5774 15.9475 41.7617 15.8553 c 42.7136 15.5173 45.815 14.5955 49.4999 15.1486 c 50.0526 15.2407 51.8029 12.3216 53.9831 12.5367 c 56.4704 12.7825 57.668 12.9055 58.5892 14.9027 c 59.3876 16.6542 59.2033 17.4839 58.7734 18.5286 c 58.6506 18.8051 58.712 19.1431 58.8963 19.3582 c 59.6332 20.2801 61.5371 22.8304 62.8882 26.3026 c 64.5464 30.5123 63.7787 33.708 63.0417 36.3506 c 62.9803 36.5964 63.011 36.8422 63.1645 37.0573 c 64.4235 38.7166 70.1658 46.5214 71.3633 50.9154 c 72.7145 55.8625 73.39 57.614 70.6264 58.0749 c 67.8627 58.5051 66.9108 59.7956 63.748 56.0162 c 60.9229 52.6054 57.1152 50.9154 57.8522 51.407 c 59.5718 53.0049 l 59.6025 52.9741 59.7868 53.0971 59.8175 53.1278 c +f +1 1 1 rg +44.4024 16.6852 m 44.4024 16.6852 33.0407 19.5428 31.9353 26.6716 c 30.4613 36.1664 32.3652 42.0354 41.4238 44.8009 c 51.8028 47.9965 60.5236 40.7141 61.5062 35.8284 c 62.4889 30.9428 62.4275 24.9202 57.3301 19.7272 c 52.2327 14.5343 44.4024 16.6852 44.4024 16.6852 c +f +32.3346 49.2254 m 32.2118 49.1333 34.3613 53.896 35.0676 54.3262 c 35.7738 54.7257 38.5682 55.7089 39.3666 55.3709 c 39.3666 55.3709 37.8619 52.2675 39.6122 49.2562 c 39.6122 49.2562 36.9714 49.2254 36.1116 51.6529 c 36.1423 51.6222 33.8393 50.1165 32.3346 49.2254 c +f +0.9647 0.9608 0.9255 rg +32.9181 50.6698 m 32.9181 50.6698 37.7699 54.5415 38.906 54.8181 c 38.906 54.8181 39.0903 55.617 38.9367 53.1895 c 38.7832 50.762 39.7658 48.1809 40.902 47.8429 c 42.0382 47.5049 36.9101 47.9044 36.603 49.9938 c 36.2959 52.0833 35.4975 49.8709 34.5763 48.7033 c 33.6858 47.5049 32.8567 45.9071 32.8874 47.2591 c 32.9181 48.6111 31.6899 49.1949 32.9181 50.6698 c +f +0.1569 0.1529 0.1529 rg +50.6666 56.6614 m 51.5571 57.3988 58.6197 60.8403 60.2472 60.7481 c 61.9054 60.656 65.2832 61.1476 65.1296 60.7481 c 65.0068 60.3487 56.593 53.1277 53.6145 51.6221 c 50.9122 50.2701 50.206 49.6862 46.8282 49.1946 c 46.6439 49.1639 43.8189 48.7951 42.9591 49.4404 c 42.4678 49.8399 41.7001 51.2226 42.4064 51.7757 c 43.2662 52.4824 46.7975 53.4964 49.1312 55.7703 c 49.2233 55.8625 49.3462 55.9546 49.469 56.0161 c 50.6666 56.6614 l +f +61.6905 39.2389 m 61.6905 39.2389 58.6198 41.9736 59.4181 43.3871 c 60.2165 44.8006 64.0242 51.069 65.6517 53.3121 c 67.2792 55.5552 68.5382 56.4156 69.3058 56.139 c 70.0735 55.8625 68.6917 49.6555 66.1123 45.0157 c 63.5329 40.4065 62.3353 38.6243 61.6905 39.2389 c +f +14.1562 28.9145 m 14.2483 28.9453 19.9598 29.4369 22.2629 29.99 c 24.5659 30.5431 26.6233 31.3727 27.2374 29.3447 c 27.8516 27.3167 29.2948 25.9647 28.8342 25.5345 c 28.3736 25.1043 24.5659 25.1658 21.2495 25.8418 c 17.9332 26.5178 11.3925 27.9005 14.1562 28.9145 c +f +0 0 0 rg +41.1782 33.2165 m 41.1782 33.2165 42.7749 34.6299 44.7402 34.4763 c 46.7054 34.3227 47.5652 33.0014 48.7628 31.3114 c 49.9604 29.6214 50.2982 27.9621 49.5919 26.3335 c 48.8856 24.7049 47.6574 23.568 44.3103 24.244 c 40.9632 24.92 39.7349 31.3114 41.1782 33.2165 c +f +57.4529 30.1128 m 57.4529 30.1128 56.4088 28.8222 55.4569 27.8697 c 54.505 26.9479 54.7199 25.1964 55.7333 24.3053 c 56.7466 23.4142 58.0977 23.2605 59.5409 26.1489 c 60.9842 29.0066 61.0456 32.1101 60.1244 34.261 c 60.1551 34.261 58.7118 31.3112 57.4529 30.1128 c +f +48.2101 36.1354 m 48.2101 36.1354 49.4077 37.7332 51.2194 38.2248 c 53.0005 38.7165 54.6586 38.0712 55.1192 37.5796 c 55.5491 37.1187 56.1326 36.1661 54.7508 35.4901 c 53.3689 34.8141 52.2021 33.9844 53.0926 33.1548 c 53.9831 32.3252 58.2207 32.3559 58.5891 33.0012 c 58.9576 33.6464 59.0497 34.1995 57.7908 35.2135 c 56.5318 36.2275 56.2247 36.8114 56.8388 37.5181 c 57.4837 38.2248 58.4356 37.7332 58.4356 37.7332 c 58.4356 37.7332 57.7908 38.8087 56.8081 38.6243 c 55.8562 38.4399 56.3782 37.979 55.8562 38.4399 c 55.3035 38.9008 54.4744 39.4539 53.3382 39.5154 c 52.2021 39.5768 50.3903 39.4539 48.9164 38.0712 c 47.4425 36.7192 47.7495 36.4426 47.8724 36.3197 c 47.9952 36.1968 48.0259 36.1661 48.2101 36.1354 c +f +0.4235 0.7804 0.8863 rg +0 64.7428 m 0 64.7428 15.8756 52.083 46.3063 67.9999 c 46.3063 67.9999 20.4816 43.848 0 64.7428 c +f +0 0.4941 0.6667 rg +4.6982 59.2118 m 4.6982 59.2118 15.6607 48.9488 32.089 57.1223 c 48.5173 65.2958 60.8308 75.651 82.5715 59.2118 c 82.5715 59.2118 64.0551 66.74 47.5654 57.2145 c 31.0757 47.689 17.1346 44.3704 4.6982 59.2118 c +f +1 1 1 rg +47.6576 32.5403 m 48.4378 32.5403 49.0702 31.9075 49.0702 31.1268 c 49.0702 30.3462 48.4378 29.7134 47.6576 29.7134 c 46.8775 29.7134 46.2451 30.3462 46.2451 31.1268 c 46.2451 31.9075 46.8775 32.5403 47.6576 32.5403 c +f +58.0113 30.8234 m 58.7908 30.7915 59.3945 30.0782 59.3598 29.2304 c 59.3251 28.3826 58.665 27.7212 57.8855 27.7531 c 57.106 27.7851 56.5023 28.4983 56.537 29.3462 c 56.5717 30.194 57.2318 30.8554 58.0113 30.8234 c +f +0.9294 0.9294 0.9294 rg +38.5068 19.0513 m 38.5068 19.0513 33.6858 27.0711 35.0062 32.9401 c 35.4361 34.8145 36.5108 43.7869 53.3076 44.1864 c 53.3076 44.1864 48.3945 46.8904 41.4547 44.8009 c 34.5149 42.7115 32.2733 38.9627 31.6591 34.4457 c 31.045 29.9288 31.9969 24.3057 34.0236 22.462 c 36.0502 20.6184 36.5109 20.0346 38.5068 19.0513 c +f +Q +BT +/F1 38 Tf +0.1333 0.1333 0.1333 rg +540 410 Td +(No screenshot available,) Tj +0 -48 Td +(Lightpanda has no graphical rendering engine.) Tj +ET +endstream +endobj +5 0 obj +<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >> +endobj +xref +0 6 +0000000000 65535 f +0000000015 00000 n +0000000064 00000 n +0000000121 00000 n +0000000249 00000 n +0000006331 00000 n +trailer +<< /Size 6 /Root 1 0 R >> +startxref +6428 +%%EOF