pdf method

Future<Uint8List> pdf({
  1. bool? blockAds,
  2. CDPLaunchOptionsOrString? launch,
  3. String? profile,
  4. num? timeout,
  5. String? token,
  6. String? trackingId,
  7. Map<String, dynamic>? body,
})

HTTP Method: POST A JSON-based API for getting a PDF binary from either a supplied "url" or "html" payload in your request. Many options exist for injecting cookies, request interceptors, user-agents and waiting for selectors, timers and more.

Note: This endpoint is also available at: /pdf for backwards compatibility. Summary: /chromium/pdf

Implementation

Future<Uint8List> pdf({
  bool? blockAds,
  CDPLaunchOptionsOrString? launch,
  String? profile,
  num? timeout,
  String? token,
  String? trackingId,
  Map<String, dynamic>? body,
}) async {
  final queryParams = <String, String>{};
  if (defaultToken != null) {
    queryParams['token'] = defaultToken!;
  }
  if (blockAds != null) {
    queryParams['blockAds'] = blockAds.toString();
  }
  if (launch != null) {
    queryParams['launch'] = launch.toString();
  }
  if (profile != null) {
    queryParams['profile'] = profile.toString();
  }
  if (timeout != null) {
    queryParams['timeout'] = timeout.toString();
  }
  if (token != null) {
    queryParams['token'] = token.toString();
  }
  if (trackingId != null) {
    queryParams['trackingId'] = trackingId.toString();
  }
  final pathUrl = '/chromium/pdf';
  final uri = Uri.parse(
    '$baseUrl$pathUrl',
  ).replace(queryParameters: queryParams.isNotEmpty ? queryParams : null);
  final request = http.Request('POST', uri);
  if (body != null) {
    request.headers['Content-Type'] = 'application/json';
    request.body = jsonEncode(body);
  }
  final streamedResponse = await _client.send(request);
  final response = await http.Response.fromStream(streamedResponse);
  if (response.statusCode != 200 && response.statusCode != 204) {
    throw Exception('HTTP ${response.statusCode}: ${response.body}');
  }
  if (response.statusCode == 204) return Uint8List(0);
  return response.bodyBytes;
}