feat: page.pdf() (#39)

This commit is contained in:
Yury Semikhatsky 2020-10-23 14:25:58 -07:00 committed by GitHub
parent f38a1d15cc
commit 114fd54060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 2 deletions

View File

@ -83,7 +83,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
} }
@Override @Override
public Browser browser() { public BrowserImpl browser() {
return browser; return browser;
} }

View File

@ -96,8 +96,17 @@ class BrowserImpl extends ChannelOwner implements Browser {
return page; return page;
} }
private String name() {
return initializer.get("name").getAsString();
}
boolean isChromium() {
return "chromium".equals(name());
}
@Override @Override
public String version() { public String version() {
return initializer.get("version").getAsString(); return initializer.get("version").getAsString();
} }
} }

View File

@ -491,7 +491,20 @@ public class PageImpl extends ChannelOwner implements Page {
@Override @Override
public byte[] pdf(PdfOptions options) { public byte[] pdf(PdfOptions options) {
return new byte[0]; if (!browserContext.browser().isChromium()) {
throw new RuntimeException("Page.pdf only supported in headless Chromium");
}
if (options == null) {
options = new PdfOptions();
}
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
params.remove("path");
JsonObject json = sendMessage("pdf", params).getAsJsonObject();
byte[] buffer = Base64.getDecoder().decode(json.get("pdf").getAsString());
if (options.path != null) {
Utils.writeToFile(buffer, options.path);
}
return buffer;
} }
@Override @Override

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import static org.junit.jupiter.api.Assertions.*;
public class TestPdf extends TestBase {
@Test
void shouldBeAbleToSaveFile() throws IOException {
// TODO: test.skip(headful || browserName !== "chromium", "Printing to pdf is currently only supported in headless chromium.");
File path = File.createTempFile("output", ".pdf");
page.pdf(new Page.PdfOptions().withPath(path));
long size = Files.size(path.toPath());
assertTrue(size > 0);
Browser b = playwright.webkit().launch();
System.out.println(b.version());
}
@Test
void shouldOnlyHavePdfInChromium() {
// TODO: test.skip(browserName === "chromium");
try {
page.pdf();
if (isChromium) {
return;
}
fail("did not throw");
} catch (RuntimeException e) {
assertFalse(e.getMessage().contains("did not throw"));
}
}
}