dart-pdf Flutter pdf Center and fullscreen preview documents with only one page

445 Views Asked by At

I'm using Flutter pdf and Printing to create and preview Pdf's documents. In my case, these documents have only one page each so, I want them to show at center in a fullscreen default mode.

I have notice that if I double tap a page, this one gets centered in the screen and you can do a zoom-in.

How can I put my documents in that state by default? Without having to double tap them in first place.

import 'package:flutter/material.dart';
import 'package:printing/printing.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;

class PdfPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: PdfPreview(
            previewPageMargin: const EdgeInsets.all(0),
            useActions: false,
            build: (format) {
              final pdf = pw.Document();

              pdf.addPage(
                pw.Page(
                  build: (pw.Context context) => pw.Center(
                    child: pw.Text('Hello World!'),
                  ),
                ),
              );

              return pdf.save();
            },
          ),
        ),
      ),
    );
  }
}

enter image description here

1

There are 1 best solutions below

0
On

Use enableScrollToPage: true to center the page on the screen when displayed.

   PdfPreview(
      enableScrollToPage: true,
      // Other properties...
    ),