Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

html - PDF::FromHTML creates empty PDF

I'm using Perls PDF::FromHTML to create a PDF-file. My code looks like this:

open HTML, ">", "file.html";
...
close HTML;
chmod(0777, "file.html");

my $pdf = PDF::FromHTML->new(encoding => 'utf-8');
$pdf->load_file("file.html") or die $!;

$pdf->convert(
        Font => 'Arial',
        LineHeight => 10,
        Landscape => 1
);

$pdf->write_file("file.pdf") or die $!;

Since I had difficulties creating an actual PDF-file at the beginning, I'm now exactly following the synopsis on cpan, which is

my $pdf = PDF::FromHTML->new( encoding => 'utf-8' );

# Loading from a file:
$pdf->load_file('source.html');

# Perform the actual conversion:
$pdf->convert(
    # With PDF::API2, font names such as 'traditional' also works
    Font        => 'font.ttf',
    LineHeight  => 10,
    Landscape   => 1,
);
# Write to a file:
$pdf->write_file('target.pdf');

However this creates a PDF-file at the right location, but it only consists of a white page. The HTML-file is complete and looks like it should look. What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to specify a valid font. The second example has a non valid fontname.

If you installed PDF::API2 you could try to use: 'Helvetica' as fontname.

Please check you HTML file, it should be quite simple (no CSS or javascript).

Regards,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...