mirror of
https://github.com/unidoc/unihtml.git
synced 2025-04-24 13:48:52 +08:00
107 lines
12 KiB
Go
107 lines
12 KiB
Go
//
|
|
// Copyright 2020 FoxyUtils ehf. All rights reserved.
|
|
//
|
|
// This is a commercial product and requires a license to operate.
|
|
// A trial license can be obtained at https://unidoc.io
|
|
//
|
|
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
|
|
//
|
|
// Use of this source code is governed by the UniDoc End User License Agreement
|
|
// terms that can be accessed at https://unidoc.io/eula/
|
|
|
|
// Package unihtml contains a plugin for the UniDoc.
|
|
package unihtml ;import (_ba "bytes";_b "context";_e "errors";_ca "fmt";_bab "github.com/unidoc/unihtml/client";_g "github.com/unidoc/unihtml/content";_fe "github.com/unidoc/unihtml/sizes";_bd "github.com/unidoc/unipdf/v3/common";_ee "github.com/unidoc/unipdf/v3/common/license";
|
|
_ef "github.com/unidoc/unipdf/v3/creator";_af "github.com/unidoc/unipdf/v3/model";_cf "github.com/unidoc/unipdf/v3/render";_fc "image";_c "image/color";_f "math";_db "net/url";_d "os";_dbe "time";);func (_fcf *Document )validate ()error {if _gb ==nil {return ErrNoClient ;
|
|
};if _fcf ._ea ==nil {return ErrContentNotDefined ;};return nil ;};func _fg (_baec _fc .Image )float64 {_gcc :=_baec .Bounds ();var (_gff int ;_df _c .Color ;_ebg ,_gcg ,_efbd uint32 ;);_bgd ,_bcc :=_gcc .Min .X ,_gcc .Max .Y -1;_bccf :=_baec .At (_bgd ,_bcc );
|
|
_bca ,_ab ,_gcf ,_ :=_bccf .RGBA ();_ggd :=_bca ==_f .MaxUint16 &&_ab ==_f .MaxUint16 &&_gcf ==_f .MaxUint16 ;for _bcc =_gcc .Max .Y -1;_bcc >=_gcc .Min .Y ;_bcc --{var _aec bool ;for _bgd =_gcc .Min .X ;_bgd < _gcc .Max .X ;_bgd ++{_df =_baec .At (_bgd ,_bcc );
|
|
_ebg ,_gcg ,_efbd ,_ =_df .RGBA ();if (_ggd &&!(_ebg ==_bca &&_gcg ==_ab &&_gcf ==_efbd ))||(!_ggd &&(_f .Abs (float64 (_ebg )-float64 (_bca ))/float64 (_f .MaxUint16 )> 0.03||_f .Abs (float64 (_gcg )-float64 (_ab ))/float64 (_f .MaxUint16 )> 0.03||_f .Abs (float64 (_efbd )-float64 (_gcf ))/float64 (_f .MaxUint16 )> 0.03)){_aec =true ;
|
|
break ;};};if _aec {break ;};_gff =_bcc ;};return float64 (_gcc .Max .Y -_gff )/float64 (_gcc .Max .Y );};func (_bed *Document )extract (_gda _b .Context ,_ecb ,_bee _fe .Length ,_aecc margins )([]*_af .PdfPage ,error ){_bcg :=_bab .BuildHTMLQuery ().SetContent (_bed ._ea ).PageSize (_bed ._cd ).PaperWidth (_ecb ).PaperHeight (_bee ).Orientation (_bed ._gd ).MarginLeft (_aecc .Left ).MarginRight (_aecc .Right ).MarginTop (_aecc .Top ).MarginBottom (_aecc .Bottom );
|
|
_fgb ,_gdc :=_bcg .Query ();if _gdc !=nil {return nil ,_gdc ;};_gda ,_ge :=_b .WithTimeout (_gda ,_dbe .Second *15);defer _ge ();_egfa ,_gdc :=_gb .ConvertHTML (_gda ,_fgb );if _gdc !=nil {return nil ,_gdc ;};_bbb :=_ba .NewReader (_egfa .Data );_aag ,_gdc :=_af .NewPdfReader (_bbb );
|
|
if _gdc !=nil {return nil ,_gdc ;};return _aag .PageList ,nil ;};
|
|
|
|
// ConnectOptions creates UniHTML HTTP Client and tries to establish connection with the server.
|
|
func ConnectOptions (o Options )error {_gb =_bab .New (_bab .Options {Hostname :o .Hostname ,Port :o .Port ,HTTPS :o .Secure });_bg ,_gc :=_b .WithTimeout (_b .Background (),_dbe .Second *5);defer _gc ();if _cfg :=_gb .HealthCheck (_bg );_cfg !=nil {return _cfg ;
|
|
};return nil ;};func _cg ()error {_gbd :=_ee .GetLicenseKey ();if _gbd ==nil {return _e .New ("\u006e\u006f\u0020\u006cic\u0065\u006e\u0073\u0065\u0020\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064");};if !_gbd .IsLicensed (){return _e .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u006f\u0072 \u006e\u006f\u0020\u006c\u0069\u0063\u0065n\u0073\u0065\u0020\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064");
|
|
};if !_gbd .UniHTML {return _e .New ("\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064\u0020l\u0069\u0063e\u006e\u0073\u0065\u0020\u0069\u0073 \u006e\u006f\u0074\u0020\u0076\u0061\u006c\u0069d\u0020\u0066\u006f\u0072\u0020\u0074\u0068\u0065\u0020U\u006e\u0069\u0048\u0054\u004d\u004c\u0020\u0065\u0078\u0074\u0065n\u0073\u0069\u006f\u006e.\u0020\u0056i\u0073i\u0074\u003a\u0020\u0068\u0074\u0074\u0070s\u003a/\u002f\u0077\u0077\u0077\u002e\u0075\u006e\u0069\u0064\u006f\u0063\u002e\u0069\u006f\u0020\u0061\u006ed\u0020c\u006f\u006e\u0074\u0061\u0063\u0074\u0020\u0073\u0075\u0070\u0070\u006f\u0072t");
|
|
};return nil ;};
|
|
|
|
// GetPdfPages is a function that converts provided input content and
|
|
func (_ff *Document )GetPdfPages (ctx _b .Context )([]*_af .PdfPage ,error ){if _dc :=_ff .validate ();_dc !=nil {return nil ,_dc ;};return _ff .extract (ctx ,_ff ._dbb ,_ff ._gcd ,_ff .getMargins ());};
|
|
|
|
// SetMarginLeft sets the left margin.
|
|
func (_dg *Document )SetMarginLeft (margin _fe .Length ){_dg ._cc .Left =margin };
|
|
|
|
// SetPageSize sets the page default size.
|
|
func (_bcb *Document )SetPageSize (pageSize _fe .PageSize )error {if !pageSize .IsAPageSize (){return _e .New ("\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064\u0020\u0069\u006ev\u0061\u006c\u0069\u0064\u0020\u0070\u0061\u0067\u0065\u0020s\u0069\u007a\u0065");
|
|
};_bcb ._cd =pageSize ;_bcb ._cfb =_ef .PositionAbsolute ;return nil ;};
|
|
|
|
// SetLandscapeOrientation sets document landscape page orientation.
|
|
func (_dfg *Document )SetLandscapeOrientation (){_dfg ._gd =_fe .Landscape };type margins struct{Left ,Right ,Bottom ,Top _fe .Length ;};
|
|
|
|
// SetPageHeight sets the page height for given document.
|
|
func (_baa *Document )SetPageHeight (pageHeight _fe .Length )error {_baa ._gcd =pageHeight ;_baa ._cfb =_ef .PositionAbsolute ;return nil ;};var ErrNoClient =_e .New ("\u0055n\u0069\u0048\u0054\u004d\u004c\u0020\u0063\u006c\u0069\u0065\u006et\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064");
|
|
|
|
|
|
// SetMargins sets the Document Margins.
|
|
func (_dd *Document )SetMargins (left ,right ,top ,bottom float64 ){_dd ._cc .Left =_fe .Point (left );_dd ._cc .Right =_fe .Point (right );_dd ._cc .Top =_fe .Point (top );_dd ._cc .Bottom =_fe .Point (bottom );_dd ._cfb =_ef .PositionAbsolute ;};
|
|
|
|
// Document is HTML document wrapper that is used for extracting and converting HTML document into PDF pages.
|
|
type Document struct{_ea _g .Content ;_cc margins ;_cfb _ef .Positioning ;_eg ,_fb float64 ;_cd _fe .PageSize ;_dbb ,_gcd _fe .Length ;_gd _fe .Orientation ;_eb bool ;};var _gb *_bab .Client ;
|
|
|
|
// SetMarginRight sets the left margin.
|
|
func (_ded *Document )SetMarginRight (margin _fe .Length ){_ded ._cc .Right =margin };func (_cfa *Document )getMargins ()margins {_afa :=_cfa ._cc ;if _cfa ._cfb .IsRelative (){_afa .Top =_fe .Millimeter (1);_afa .Left =_fe .Millimeter (1);_afa .Bottom =_fe .Millimeter (1);
|
|
_afa .Right =_fe .Millimeter (1);return _afa ;};if _afa .Top ==nil {_afa .Top =_fe .Millimeter (10);};if _afa .Bottom ==nil {_afa .Bottom =_fe .Millimeter (10);};if _afa .Left ==nil {_afa .Left =_fe .Millimeter (10);};if _afa .Right ==nil {_afa .Right =_fe .Millimeter (10);
|
|
};return _afa ;};
|
|
|
|
// Options are the HTML Client options used for establishing the connection.
|
|
type Options struct{
|
|
|
|
// Hostname defines the hostname for the client. Default value: 'localhost'.
|
|
Hostname string ;
|
|
|
|
// Port defines the port at which the server works. Default value: '8080'
|
|
Port int ;
|
|
|
|
// Secure is the flag that states if the connection uses HTTPS protocol. Default Value: 'false'.
|
|
Secure bool ;
|
|
|
|
// Prefix is an option setting used when the server is working with the URI prefix. Default Value: ''.
|
|
Prefix string ;};
|
|
|
|
// SetPos sets the absolute position. Changes object positioning to absolute.
|
|
func (_aeb *Document )SetPos (x ,y float64 ){_aeb ._cfb =_ef .PositionAbsolute ;_aeb ._eg =x ;_aeb ._fb =y ;};
|
|
|
|
// SetMarginTop sets the left margin.
|
|
func (_fdg *Document )SetMarginTop (margin _fe .Length ){_fdg ._cc .Top =margin };
|
|
|
|
// SetPageWidth sets the page width for given document.
|
|
func (_bf *Document )SetPageWidth (pageWidth _fe .Length )error {_bf ._dbb =pageWidth ;_bf ._cfb =_ef .PositionAbsolute ;return nil ;};
|
|
|
|
// Connect creates UniHTML HTTP Client and tries to establish connection with the server.
|
|
func Connect (path string )error {if _caa :=_cg ();_caa !=nil {return _caa ;};_bac ,_ae :=_bab .ParseOptions (path );if _ae !=nil {return _ae ;};_gb =_bab .New (_bac );_be ,_bad :=_b .WithTimeout (_b .Background (),_dbe .Second *5);defer _bad ();if _efb :=_gb .HealthCheck (_be );
|
|
_efb !=nil {return _efb ;};return nil ;};
|
|
|
|
// GeneratePageBlocks implements creator.Drawable interface.
|
|
func (_bb *Document )GeneratePageBlocks (ctx _ef .DrawContext )([]*_ef .Block ,_ef .DrawContext ,error ){if _beca :=_bb .validate ();_beca !=nil {return nil ,ctx ,_beca ;};var _ac []*_ef .Block ;_dcg :=_bb .getMargins ();_ebd ,_aca :=_bb ._dbb ,_bb ._gcd ;
|
|
if _bb ._cfb .IsRelative (){_ebd ,_aca =_fe .Point (ctx .Width ),_fe .Point (ctx .Height );ctx .X -=float64 (_dcg .Left .Points ());};_beb ,_bc :=_bb .extract (_b .Background (),_ebd ,_aca ,_dcg );if _bc !=nil {return nil ,_ef .DrawContext {},_bc ;};for _ffg ,_fd :=range _beb {_bae ,_gf :=_ef .NewBlockFromPage (_fd );
|
|
if _gf !=nil {return nil ,_ef .DrawContext {},_gf ;};var _ebc float64 ;if _bb ._eb &&_ffg ==len (_beb )-1{_gba :=_cf .NewImageDevice ();_fff ,_cgb :=_gba .Render (_fd );if _cgb !=nil {return nil ,_ef .DrawContext {},_cgb ;};_egf ,_cgb :=_fd .GetMediaBox ();
|
|
if _cgb !=nil {return nil ,_ef .DrawContext {},_cgb ;};_aa :=_dbe .Now ();_gg :=_fg (_fff );_ebc =_egf .Height ()*_gg ;_bd .Log .Trace ("\u0054\u0072i\u006d\u006d\u0069\u006eg\u0020\u006ca\u0073\u0074\u0020\u0064\u006f\u0063\u0075\u006de\u006e\u0074\u0020\u0070\u0061\u0067\u0065\u0020\u0074\u0061\u006b\u0065n\u003a\u0020\u0025\u0076",_dbe .Since (_aa ));
|
|
if _bb ._cc .Bottom !=nil {_ebc -=float64 (_bb ._cc .Bottom .Points ());};if _ebc < 0{_ebc =0;};_bd .Log .Trace ("C\u0072\u006f\u0070\u0070\u0069\u006e\u0067\u0020\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074\u0027\u0073\u0020\u0070\u0061\u0067\u0065\u0020\u0025\u002e2\u0066 \u0070\u006f\u0069\u006et\u0073\u0020o\u0066\u0066\u0020\u0062\u006f\u0074\u0074\u006f\u006d\u0020\u006f\u0066\u0020\u006d\u0065\u0064\u0069\u0061\u0020\u0062\u006f\u0078\u000a",_ebc );
|
|
};_adf ,_fcc ,_gf :=_bae .GeneratePageBlocks (ctx );if _gf !=nil {return nil ,_ef .DrawContext {},_gf ;};ctx =_fcc ;ctx .Y -=_ebc ;if _ffg !=len (_beb )-1&&ctx .Y > (ctx .PageHeight -ctx .Margins .Bottom )*.95{ctx .X =ctx .Margins .Left ;ctx .Y =ctx .Margins .Top ;
|
|
ctx .Page ++;};_ac =append (_ac ,_adf ...);};return _ac ,ctx ,nil ;};var ErrContentNotDefined =_e .New ("\u0068\u0074\u006d\u006c\u0020\u0064o\u0063\u0075\u006d\u0065\u006e\u0074\u0020\u0063\u006f\u006e\u0074\u0065\u006et\u0020\u006e\u006f\u0074\u0020\u0064\u0065f\u0069\u006e\u0065\u0064");
|
|
|
|
|
|
// TrimLastPageContent trims the last page content so that next creator blocks are located just at the end of given block.
|
|
func (_egg *Document )TrimLastPageContent (){_egg ._eb =true };
|
|
|
|
// SetMarginBottom sets the left margin.
|
|
func (_ace *Document )SetMarginBottom (margin _fe .Length ){_ace ._cc .Bottom =margin };var _ _ef .Drawable =(*Document )(nil );
|
|
|
|
// NewDocument creates new HTML Document used as an input for the creator.Drawable.
|
|
func NewDocument (path string )(*Document ,error ){_eaa :=&Document {};_bec ,_aed :=_db .Parse (path );if _aed !=nil {return nil ,_aed ;};switch _bec .Scheme {case "\u0068\u0074\u0074\u0070","\u0068\u0074\u0074p\u0073":_eaa ._ea ,_aed =_g .NewWebURL (path );
|
|
if _aed !=nil {return nil ,_aed ;};return _eaa ,nil ;};_ec ,_aed :=_d .Stat (path );if _aed !=nil {return nil ,_aed ;};if !_ec .IsDir (){_eaa ._ea ,_aed =_g .NewHTMLFile (path );}else {_eaa ._ea ,_aed =_g .NewZipDirectory (path );};if _aed !=nil {return nil ,_aed ;
|
|
};return _eaa ,nil ;};
|
|
|
|
// ChapterDraw implements creator.containerElement interface.
|
|
func (_dce *Document )ContainerComponent (container _ef .Drawable )(_ef .Drawable ,error ){switch container .(type ){case *_ef .Chapter :default:return nil ,_ca .Errorf ("\u0075\u006e\u0069\u0068t\u006d\u006c\u002e\u0044\u006f\u0063\u0075\u006d\u0065n\u0074\u0020\u0063\u0061\u006e\u0027\u0074\u0020\u0062\u0065\u0020\u0061\u0020\u0063\u006f\u006d\u0070\u006fn\u0065\u006e\u0074\u0020\u006ff\u0020\u0074\u0068\u0065\u0020\u0025\u0054\u0020\u0063\u006f\u006e\u0074\u0061\u0069\u006e\u0065\u0072",container );
|
|
};return _dce ,nil ;}; |