#1 2010-07-14 17:04:51

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

htm2pdf free tool release

After a discussion with Pawel Stroinski on this forum, the THtmlView component was used to make a tool converting any html document into pdf, i.e. "printing" a html document as a pdf.

I downloaded via SVN the latest THtmlView source code available from the successor of the well-known HTMLViewer components for Delphi (previously sold by PBear.com), i.e. r36.

Then I compiled it under Delphi 7. Some errors in the code. I therefore fixed them. Then try to compile under Delphi 2010 - a lot of warnings, and some main feature not working as expected (like css support). Fixed most bugs, and made a lot of code refactoring - a lot of warning remains, and the component is not truly Unicode: THtmlView use internaly UTF-8 encoding. But it works better than the original version.

Then I used Pawel Stroinski's code using our SynPdf unit to produce a pdf file from any html content.

Some bitmaps where not printed: fixed some part of the SynPdf library (now in version 1.8.3 in our source code repository).

And since the code from THtmlView did rely on jpeg.pas and was handling PNG just with Delphi 2010, I added a compilation define (set by default) in order to use our SynGdiPlus library to read GIF, PNG and JPEG files. So the resulting executable is smaller in size (less than 890 KB), and work as expected. If you don't like all this, and need GIF animated in your PDF (??? it's a joken pdf doesn't allow this), you can turn the compilation define OFF, and use the previous code.

I've uploaded the source code in the HtmlView sub directory of our Source Code Repository.

You can download an executable file from http://synopse.info/files/pdf/htm2pdf.zip

Just extract the htm2pdf.exe file in a directory, then launch it:

htm2pdf.png

Click on the "Open" button to open a html file, then browse it on the screen, the the parameters below (like margins or paper size), and click on the "Save as" or "Save" buttons.

A pdf file will be created, containing the whole html content, together with the files.

As a demo, I included in the HtmlView/demo sub directory of our Source Code Repository some html, css and picture files.

You can download the resulting pdf from http://synopse.info/files/pdf/sample.pdf

This htm2pdf tool can be used from the command line, without the GUI window above:

1. you'll observe that the last used parameters are saved in a htm2pdf.ini file in the same directory than the executable.

If you launch

htm2pdf /silent

all parameters from htm2pdf.ini will be used, no GUI will be displayed, and the conversion will be made.

2. you can force to specify a html source file name, and an optional destination pdf file:

htm2pdf sample.htm

will create a sample.pdf file in the same directory as sample.htm

htm2pdf sample.htm dest.pdf

will create a dest.pdf file

This tool is released under the Public Domain, free but with no warranty at all.

Thanks Pawel for your idea, debugging and gui code!

Offline

#2 2010-07-14 18:49:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

Here below is the main conversion loop for this htm2pdf tool:

procedure THtml2Pdf.Execute;
var
  LFormatWidth, LWidth, LHeight, LI: Integer;
  LPages: TList;
  LPage: TMetafile;
  LScale: Single;
begin
  LFormatWidth := Points2Pixels(DefaultPageWidth -
    Centimeters2Points(MarginLeft + MarginRight));
  LWidth := Viewer.FullDisplaySize(LFormatWidth).cx;
  if ScaleToFit and (LWidth > LFormatWidth) and (LFormatWidth > 0) then
    LScale := LFormatWidth / LWidth
  else
    LScale := 1;
  LHeight := Points2Pixels((DefaultPageHeight - Centimeters2Points(MarginTop + MarginBottom)) / LScale);
  LPages := Viewer.MakePagedMetaFiles(LWidth, LHeight);
  for LI := 0 to LPages.Count - 1 do
  begin
    AddPage;
    LPage := TMetafile(LPages[LI]);
    Canvas.RenderMetaFile(LPage, LScale,
      Centimeters2Points(MarginLeft), -Centimeters2Points(MarginTop));
    FreeAndNil(LPage);
  end;
  FreeAndNil(LPages);
end;

Not so difficult to follow, isn't it?

It demonstrates the power of our Open Source SynPdf unit.
wink

Offline

#3 2010-07-15 08:52:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

I've updated the htm2pdf tool:

1. now clipping is done before drawing the page content: in some cases, THtmlView used to print something outside the margin range. In the previous resulting file, there were some "bullet lines" in the top margin of page 2. It's now fixed.

2. now you can optionally draw page number together with the file name, thanks to a new "with Page #" checkbox on the GUI.

The http://synopse.info/files/pdf/sample.pdf file now reflects both the fix (there is no bullet on page 2 any more) and the new feature (there are a page number in the document footer). Check this out!

Both source code repository and executable file have been updated.

Offline

#4 2010-07-15 16:27:43

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

Some margin problems fixed, thanks to Pawel's report.

Both source code repository and executable file have been updated.

Offline

#5 2010-10-26 18:50:51

jonhnn
Member
Registered: 2010-10-25
Posts: 2

Re: htm2pdf free tool release

I can not compile in Delphi 2006 or Delphi 7, appears several errors:

[Pascal Error] Main_Form.pas(70): E2003 Undeclared identifier: 'Viewer'
[Pascal Error] Main_Form.pas(95): E2003 Undeclared identifier: 'Viewer'
[Pascal Error] Main_Form.pas(120): E2003 Undeclared identifier: 'Viewer'
[Pascal Fatal Error] SynBz.pas(252): F1026 File not found: 'bunzipasm.inc' in unit SynBz.pas

[Pascal Error] SynBz.pas(169): E2065 Unsatisfied forward or external declaration: 'BZ2_bzDecompressInit'
[Pascal Error] SynBz.pas(170): E2065 Unsatisfied forward or external declaration: 'BZ2_bzDecompress'
[Pascal Error] SynBz.pas(171): E2065 Unsatisfied forward or external declaration: 'BZ2_bzDecompressEnd'
[Pascal Fatal Error] htm2pdf.dpr(42): F2063 Could not compile used unit '..\..\SynPDF\SynBz.pas'

in HTMLCompEdit.pas file not found: DesignEditors.dcu and DesignEditors.dcu


Does anyone know how to solve.

Thank you very much

Offline

#6 2010-10-27 08:00:39

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

You didn't get all the necessary files from the source code repository.

The .inc file was missing!

I've added the missing bunzipasm.inc file to the repository.

Thanks for your feedback.

Offline

#7 2010-11-18 22:34:53

blewis
Member
Registered: 2010-11-18
Posts: 3

Re: htm2pdf free tool release

Would you expect this to work in a windows service? I have got it working in a forms application, but not in a non-interactive mode.

I get a stack overflow in CompressStream (called from InternalWriteTo).

THtmlViewer has a form as parent, but is not visible.

Something is going wrong in here:
      TmpStream := THeapMemoryStream.Create;
      try
        TmpSize := CompressStream(TMemoryStream(FWriter.fDestStream).Memory,
          TmpSize,TmpStream,7,true);
        FLength.Value := TmpSize;
        FAttributes.WriteTo(W);
        W.Add(#10'stream'#10).Add(TmpStream.Memory,TmpSize).Add(#10'endstream');
      finally
        TmpStream.Free;
      end;

Offline

#8 2010-11-19 13:06:18

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

Try enabling the "interact with the desktop" option for your service.

About compression, there is no reason why it could stuck as a service.
I used the whole framework as a service, with compression on, without any problem, on a real world application (using the Service-related units available in the framework distribution).
With no problem.

On which OS are you running this service?

Offline

#9 2012-12-14 22:38:37

dMonkey
Member
Registered: 2012-12-14
Posts: 6

Re: htm2pdf free tool release

Hi everyone!

I know this tool is some days old, but 4 now I have a problem after installing XE on my new machine.

Everytime I convert html to pdf, everything is ok - BUT I cant see any table borders. I mean the tag is ok, like <table border="1">...</table> I can see the border in the "preview" (htmlviewer component) but in this moment when I save it as pdf, the border get invisible. please help! I have a big project which is working great but with this new buld it looses the table borders. somebody else have this problem too?

PS: emf is looking normaly... (paralel saving from same source)

Last edited by dMonkey (2012-12-15 00:53:29)

Offline

#10 2012-12-17 09:31:28

dMonkey
Member
Registered: 2012-12-14
Posts: 6

Re: htm2pdf free tool release

no one an idea?

Offline

#11 2012-12-17 17:48:56

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

HTM2PDF tool is not compatible with XE, AFAIK.

Our SynPDF unit is 100% compatible, but the THtmlView component is not.
sad

This is perhaps your problem.

Offline

#12 2012-12-18 10:48:46

dMonkey
Member
Registered: 2012-12-14
Posts: 6

Re: htm2pdf free tool release

I downloaded and installed the THTMLView component from here: http://code.google.com/p/thtmlviewer/downloads/list

it ist compatible with XE (HtmlViewer Components Version 11.3 for Delphi 6-XE2)

In the other post you are writing:

ab wrote:

spend time on the THmlView source code in order to fix some issues

Which issues were fixed? You worked on r36 I have r306, if I would know your changes maybe I can reproduce them on the newer release.

Or maybe you have another idea for using html2pfd with another browser component, maybe the standard one, is that possible?
My program is generating html reports out of access tables and upload them but I need the pdf files too for local storing.

Offline

#13 2012-12-23 10:33:33

dMonkey
Member
Registered: 2012-12-14
Posts: 6

Re: htm2pdf free tool release

Please try to help me out... Can't find a solution for this problem...

Offline

#14 2013-01-01 18:29:02

dMonkey
Member
Registered: 2012-12-14
Posts: 6

Re: htm2pdf free tool release

realy no one? sad

Offline

#15 2013-09-03 15:10:03

Cornerstone
Member
Registered: 2013-09-03
Posts: 3

Re: htm2pdf free tool release

Hi. Thank you for great library!
I'm trying to convert many HTML files to one PDF file. And I'd like to make each HTML file starts from new page in PDF file. Is there any way to make it with Synopse+THTMLView?

Offline

#16 2013-09-03 15:10:45

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

Yes, if you use the right Delphi version.

Offline

#17 2013-09-04 07:50:28

Cornerstone
Member
Registered: 2013-09-03
Posts: 3

Re: htm2pdf free tool release

I'm using XE2. I've read about the problems in THTMLViewer with XE+, but everything works good on XE2. So far I have not found any problems. Anyway, how can I do that every HTML-file starts on a new page in the PDF-file?

Offline

#18 2013-09-04 10:56:09

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

You can just call AddPage between the files.

Offline

#19 2013-09-04 13:22:53

Cornerstone
Member
Registered: 2013-09-03
Posts: 3

Re: htm2pdf free tool release

ab, Thank you! Works well.

Last edited by Cornerstone (2013-09-04 13:23:10)

Offline

#20 2014-07-30 00:38:36

RaelB
Member
Registered: 2010-08-04
Posts: 43

Re: htm2pdf free tool release

Hi,

I am using your tool html2pdf, however with the demo application the produced pdf leaves out the first heading section. Any idea how to solve this?

Using Delphi 7

Sample image:
PDF missing top heading section

Thanks
Rael

Offline

#21 2014-10-16 12:57:00

Vivi
Member
Registered: 2014-10-16
Posts: 5

Re: htm2pdf free tool release

ab wrote:

After a discussion with Pawel Stroinski on this forum, the THtmlView component was used to make a tool converting any html document into pdf, i.e. "printing" a html document as a pdf.


hi
very nice idea, how to convert HTML to PDF
i have one question, do you know how to implement page breaks in html so that pdf can break it too?

Offline

#22 2014-10-16 13:09:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

It will depend on THtmlView implementation only.
Please search on this side... but there is no standard HTML way of doing it, by design.

Offline

#23 2014-11-12 07:24:32

Vivi
Member
Registered: 2014-10-16
Posts: 5

Re: htm2pdf free tool release

ab wrote:

It will depend on THtmlView implementation only.
Please search on this side... but there is no standard HTML way of doing it, by design.

ok, solved that by making seperate htmls and then adding new page when new html starts
but now i have another problem, metafiles are created OK, but when trying to change margins, the final image is not resized, but clipped wrong?
for example, your compiled .exe works fine,but when i compile it, same error - so i guess the problem is with lib i am using? (latest synpdf and thtmlviewer)

Offline

#24 2015-04-19 16:47:29

Dmitro25
Member
Registered: 2015-02-21
Posts: 19

Re: htm2pdf free tool release

1. I think I have found a little error in htm2pdf application located inside mORMot\HtmlView\htm2pdf\Html2Pdf.pas
This file contains the following code (line #102):

LMarginY := -Centimeters2Points(MarginTop);

But I think it should be (no "-" char):

LMarginY := Centimeters2Points(MarginTop);

2. Also I have incorrectly looking PDF when trying to convert the following htm file using htm2pdf.
Source file (htm): https://drive.google.com/open?id=0Bzzn1 … authuser=0
Result (pdf): https://drive.google.com/open?id=0Bzzn1 … authuser=0
It is seen that in the pdf on the 2nd and 3rd page spoiled the table header (color shift and additional horizontal line), and on the last page from the end table before the end of the page are drawn two vertical lines. I don't know what to think, because when I print the same htm file on the printer (print using the same htmlview component), the result is obtained without these artifacts:
(result of printing to a virtual printer, tif): https://drive.google.com/open?id=0Bzzn1 … authuser=0

Offline

#25 2016-07-08 11:51:06

mogulza
Member
Registered: 2015-03-02
Posts: 13

Re: htm2pdf free tool release

I have been using nightlybuild version of the Synopse framework since last year on Delphi XE2 and only noticed the htm2pdf project today.  I was initially unable to compile because I did not have the THTMLViewer component installed. 

After getting that installed, I I got compile errors in that the TPrinterState constants could not be resolved  !!!  While TPrinterState itself could be resolved none of the psNoHandle, psHandleIC, psHandleDC contants were found !!!  I had to change all instances of these to TPrinterState(0), TPrinterState(1) and TPrinterState(2) respectively !!

Eventually was able to compile htm2pdf.exe.  However, when I use any non-zero (centimetrs) value for margins my resulting PDF looks ridiculous.

I eventually found http://blog.synopse.info/post/2010/07/1 … -free-tool page and downloaded the compiled version of htm2pdf.exe and that works correctly.

This leads me to think that either my $Defines for XE2 are incorrect OR the htm2pdf code in the nightlybuild is somehow incorrect.  Can anyone help on this?

Offline

#26 2016-07-08 12:22:13

mogulza
Member
Registered: 2015-03-02
Posts: 13

Re: htm2pdf free tool release

With respect to my post above, some more information, .... I have worked out that the "ridiculous" PDF has it's image height too large causing the pdf to look compressed and tall thereby leaving out the bottom part of my HTML from the PDF completely.

I have also made the necessary changes to SynPdf.pas to support A2, A1 and A0 paper sizes if anyone is interested.  Similarly I have added A2, A1 and A0 options to htm2pdf.exe

Now I just need to resolve the non-zero (centimeters) margin value problem

Offline

#27 2016-07-08 12:50:37

mogulza
Member
Registered: 2015-03-02
Posts: 13

Re: htm2pdf free tool release

With respect to my two posts above, some more information, .... setting the left margin to 1 and leaving the rest as 0 causes the tall compressed PDF output

Offline

#28 2016-07-08 16:22:10

mogulza
Member
Registered: 2015-03-02
Posts: 13

Re: htm2pdf free tool release

With respect to my three posts above, some more information, .... I have downloaded the latest HTMLViewer code from http://github.com/BerndGabriel/HtmlViewer and my problem still exists

Offline

#29 2022-05-27 05:48:38

TPrami
Member
Registered: 2010-07-06
Posts: 105

Re: htm2pdf free tool release

Is code/tool still available somewhere?

All links seem to point something obsolete.

-tee-

Offline

#30 2022-05-27 07:29:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: htm2pdf free tool release

The blog article is still available at https://blog.synopse.info/?post/2010/07 … -free-tool

The source is at https://synopse.info/fossil/dir?ci=tip&name=HtmlView

Made and tested on pre-Unicode compilers, i.e. Delphi 6/7/2007 only IIRC.

Offline

Board footer

Powered by FluxBB